effective java 摘選條目分享 3 - concurrency

29
2015/04/28 讀書會分享 Effective Java 摘選條目分享 3 - Concurrency Kane

Upload: kane-shih

Post on 07-Aug-2015

34 views

Category:

Software


2 download

TRANSCRIPT

Page 1: Effective java   摘選條目分享 3 - concurrency

2015/04/28 讀書會分享

Effective Java摘選條目分享 3

- Concurrency

Kane

Page 2: Effective java   摘選條目分享 3 - concurrency

大綱

Concurrency issues?atomicvolatilesynchronized 常用類別

wait / notifyThread Safety

Page 3: Effective java   摘選條目分享 3 - concurrency

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

You should already know ...

http://nerds.weddingpartyapp.com/tech/2014/06/20/primer-threading-handlers-android/

Page 5: Effective java   摘選條目分享 3 - concurrency

You should already know...

https://www3.ntu.edu.sg/home/ehchua/programming/java/J5e_multithreading.html

Page 7: Effective java   摘選條目分享 3 - concurrency

Problemclass MyThread extends Thread { private boolean stop = false;

public void run() { while(!stop) { doSomeWork(); } }

public void setStop() { this.stop = true; }

調用了setStop(),

卻可能仍然不會停下。

Page 10: Effective java   摘選條目分享 3 - concurrency

volatile 注意事項

對該變數的所有操作皆為原子操作?

http://stackoverflow.com/questions/3519664/difference-between-volatile-and-synchronized-in-java

Page 11: Effective java   摘選條目分享 3 - concurrency

考慮以下這段程式碼

public void updateCounter() {

if (counter == 1000) {

counter = 0;

} else {

counter++;

}

}

Page 12: Effective java   摘選條目分享 3 - concurrency

考慮以下這段程式碼

public void updateCounter() {

if (counter == 1000) {

counter = 0;

} else {

counter++;

}

}

從 main memory 讀取至 cache

在 cache 中更新值

在某個時機點寫回 main memory

Page 13: Effective java   摘選條目分享 3 - concurrency

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

atomic

● 不可切割

● a++; 讀取 a

更新 a

Page 15: Effective java   摘選條目分享 3 - concurrency

synchronized

http://www.ibm.com/developerworks/library/j-jtp03304/

waiting for M’s lock

Page 16: Effective java   摘選條目分享 3 - concurrency

object locking / monitor

https://www.artima.com/insidejvm/ed2/threadsynchP.html

Page 17: Effective java   摘選條目分享 3 - concurrency

synchronized 注意事項

● 避免調用外部方法i.e. Template Method Pattern, callback, sub-class’s method, ...

● 重入 (reentrant)

Page 18: Effective java   摘選條目分享 3 - concurrency

常用類別 - 資料、容器

AtomicInteger, AtomicLong …

CopyOnWriteArrayList

ConcurrentHashMap

...

Page 19: Effective java   摘選條目分享 3 - concurrency

常用類別 - 資料、容器

AtomicInteger, AtomicLong …

CopyOnWriteArrayList

ConcurrentHashMap

...

putIfAbsent(k, v)-----------------------------if (containsKey(k)) { return put(k, v);} else { return get(k);}

Page 20: Effective java   摘選條目分享 3 - concurrency

常用類別 - 控制

Executor, ExecutorService, …

CountDownLatch

Semaphore

CyclicBarrier

Page 21: Effective java   摘選條目分享 3 - concurrency

常用類別 - 控制

Executor, ExecutorService, …

CountDownLatch

Semaphore

CyclicBarrier

更方便控制的 thread pool

Page 22: Effective java   摘選條目分享 3 - concurrency

常用類別 - 控制

Executor, ExecutorService, …

CountDownLatch

Semaphore

CyclicBarrier

等到扣完才動

同時間只有 n 個能進入

等到有 n 個到達 await() 後才動

Page 23: Effective java   摘選條目分享 3 - concurrency

wait / notify

1. 應優先使用 java.util.concurrent 提供的類別

2. wait 必在 loop 中使用

3. 優先考慮 notifyAll

Page 25: Effective java   摘選條目分享 3 - concurrency

WHY: wait 必在 loop 中使用

1. notify 已經做了,先檢查條件可免 wait

2. 別人做了 notifyAll

3. spurious wakeupsynchronized (obj) {

while (<需等待的條件>) {

obj.wait();

}

}

Page 26: Effective java   摘選條目分享 3 - concurrency

spurious wake

scheduler可能因為一些原因而blackout中間可能錯失信號

故回復時會喚醒 thread,避免無限 waiting

關鍵字:POSIX Threads, Windows API, pthread_cond_wait, futex

Page 27: Effective java   摘選條目分享 3 - concurrency

優先考慮 notifyAll

1. 避免沒有收到 notify 的情況

2. 保護:wait 在 loop 中

Page 28: Effective java   摘選條目分享 3 - concurrency

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

Q&A