java 記憶體可見性問題
阿新 • • 發佈:2018-12-22
問題描述:同一個物件,在多執行緒中進行操作,會出現記憶體可見性問題。
針對這種問題,我們做了一些應對措施
1. 加volatile, 結果發現沒有效果
2. 加lock
修改程式碼如下:
發現成功了。不會出現之前的那個問題。package feedback.common.lock; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; /** * 所物件 * Created by sff on 2018/5/8. */ public class FeedBackLock { private Lock lock = new ReentrantLock(); private Condition done = lock.newCondition(); private volatile AtomicBoolean finished = new AtomicBoolean(false); private volatile boolean success; private volatile Exception cause; public FeedBackLock(){ success = false; } public void lock(long timeout) throws InterruptedException{ lock.lock(); try{ if(!finished.get()){ // 時間一到直接就過去了,不會拋異常的。 done.await(timeout, TimeUnit.MILLISECONDS); } } catch (InterruptedException e) { throw e; } finally { lock.unlock(); } } public void unLock() { lock.lock(); try{ this.success = cause == null; // 時間一到直接就過去了,不會拋異常的。 done.signal(); finished.set(true); } finally { lock.unlock(); } } public boolean isSuccess() { lock.lock(); try { return success; } finally { lock.unlock(); } } public Exception getCause() { lock.lock(); try { return cause != null ? cause : new TimeoutException("feedback timeout"); } finally { lock.unlock(); } } public void setCause(Exception cause) { this.cause = cause; } }