effective java 摘選條目分享 3 - concurrency
TRANSCRIPT
![Page 1: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/1.jpg)
2015/04/28 讀書會分享
Effective Java摘選條目分享 3
- Concurrency
Kane
![Page 2: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/2.jpg)
大綱
Concurrency issues?atomicvolatilesynchronized 常用類別
wait / notifyThread Safety
![Page 3: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/3.jpg)
You should already know ...
http://stackoverflow.com/questions/200469/what-is-the-difference-between-a-process-and-a-thread
![Page 4: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/4.jpg)
You should already know ...
http://nerds.weddingpartyapp.com/tech/2014/06/20/primer-threading-handlers-android/
![Page 5: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/5.jpg)
You should already know...
https://www3.ntu.edu.sg/home/ehchua/programming/java/J5e_multithreading.html
![Page 6: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/6.jpg)
Concurrency issuesWhat is the most frequent concurrency issue you've encountered in Java?
http://stackoverflow.com/questions/461896/what-is-the-most-frequent-concurrency-issue-youve-encountered-in-java
![Page 7: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/7.jpg)
Problemclass MyThread extends Thread { private boolean stop = false;
public void run() { while(!stop) { doSomeWork(); } }
public void setStop() { this.stop = true; }
調用了setStop(),
卻可能仍然不會停下。
![Page 8: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/8.jpg)
暫存
http://javarevisited.blogspot.tw/2011/06/volatile-keyword-java-example-tutorial.html
![Page 9: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/9.jpg)
volatile
http://www.slideshare.net/caroljmcdonald/java-concurrency-memory-model-and-trends-4961797
![Page 10: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/10.jpg)
volatile 注意事項
對該變數的所有操作皆為原子操作?
http://stackoverflow.com/questions/3519664/difference-between-volatile-and-synchronized-in-java
![Page 11: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/11.jpg)
考慮以下這段程式碼
public void updateCounter() {
if (counter == 1000) {
counter = 0;
} else {
counter++;
}
}
![Page 12: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/12.jpg)
考慮以下這段程式碼
public void updateCounter() {
if (counter == 1000) {
counter = 0;
} else {
counter++;
}
}
從 main memory 讀取至 cache
在 cache 中更新值
在某個時機點寫回 main memory
![Page 13: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/13.jpg)
Case: Two threads
if (counter == 1000) { counter = 0; } else { counter++; }
if (counter == 1000) {
counter = 0; } else { counter++; }
thread 1 thread 2
![Page 14: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/14.jpg)
atomic
● 不可切割
● a++; 讀取 a
更新 a
![Page 15: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/15.jpg)
synchronized
http://www.ibm.com/developerworks/library/j-jtp03304/
waiting for M’s lock
![Page 16: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/16.jpg)
object locking / monitor
https://www.artima.com/insidejvm/ed2/threadsynchP.html
![Page 17: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/17.jpg)
synchronized 注意事項
● 避免調用外部方法i.e. Template Method Pattern, callback, sub-class’s method, ...
● 重入 (reentrant)
![Page 18: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/18.jpg)
常用類別 - 資料、容器
AtomicInteger, AtomicLong …
CopyOnWriteArrayList
ConcurrentHashMap
...
![Page 19: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/19.jpg)
常用類別 - 資料、容器
AtomicInteger, AtomicLong …
CopyOnWriteArrayList
ConcurrentHashMap
...
putIfAbsent(k, v)-----------------------------if (containsKey(k)) { return put(k, v);} else { return get(k);}
![Page 20: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/20.jpg)
常用類別 - 控制
Executor, ExecutorService, …
CountDownLatch
Semaphore
CyclicBarrier
![Page 21: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/21.jpg)
常用類別 - 控制
Executor, ExecutorService, …
CountDownLatch
Semaphore
CyclicBarrier
更方便控制的 thread pool
![Page 22: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/22.jpg)
常用類別 - 控制
Executor, ExecutorService, …
CountDownLatch
Semaphore
CyclicBarrier
等到扣完才動
同時間只有 n 個能進入
等到有 n 個到達 await() 後才動
![Page 23: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/23.jpg)
wait / notify
1. 應優先使用 java.util.concurrent 提供的類別
2. wait 必在 loop 中使用
3. 優先考慮 notifyAll
![Page 24: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/24.jpg)
https://codexplo.wordpress.com/2012/10/20/state-diagram-of-java-thread/
![Page 25: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/25.jpg)
WHY: wait 必在 loop 中使用
1. notify 已經做了,先檢查條件可免 wait
2. 別人做了 notifyAll
3. spurious wakeupsynchronized (obj) {
while (<需等待的條件>) {
obj.wait();
}
}
![Page 26: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/26.jpg)
spurious wake
scheduler可能因為一些原因而blackout中間可能錯失信號
故回復時會喚醒 thread,避免無限 waiting
關鍵字:POSIX Threads, Windows API, pthread_cond_wait, futex
![Page 27: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/27.jpg)
優先考慮 notifyAll
1. 避免沒有收到 notify 的情況
2. 保護:wait 在 loop 中
![Page 28: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/28.jpg)
Thread Safety1. immutable 不需要外部同步,因為instance狀態不變
2. unconditionally thread-safe 不需要外部同步
3. conditionally thread-safe 有些條件下必須使用外部同步
4. not thread-safe 使用者要自己控制同步
5. thread-hostile 就算都做了外部同步,也不行
![Page 29: Effective java 摘選條目分享 3 - concurrency](https://reader031.vdocuments.mx/reader031/viewer/2022020800/55c3e307bb61ebf7538b4648/html5/thumbnails/29.jpg)
Q&A