保持java線程間可見性的5種方式
Primitive | Writes up to and including… | ...are made visible to… |
---|---|---|
Object | the end of a synchronized block or method | a thread entering a synchronized block or method for the same object. |
Volatile field | a write to a volatile field | any thread reading that volatile field. |
Thread | a call to Thread.start |
the newly started thread. |
Thread | the final write made by a dying thread | any thread which successfully calls Thread.join on that thread. |
Final field | the initialization of a final field (but only those writes affecting the field and any object it references) | any thread, provided that the constructor of the object containing the field doesn’t write the value of this anywhere eventually visible to other threads |
這些在《java並發編程實踐》一書中已經有詳細解釋,特別是對於ReentrantLock、volatile域以及final域的討論。今天在MenTaLguY的blog上看到這張表,摘錄下
文章轉自莊周夢蝶 ,原文發布時間2008-02-23
最後更新:2017-05-17 17:31:51