Java Volatile的一個實際應用場合
Consider the following example:
package thread;public class ThreadVerify {
public static boolean stop = false;
public static void main(String args[]) throws InterruptedException {
Thread testThread = new Thread(){
@Override
public void run(){
int i = 1;
while(!stop){
//System.out.println("in thread: " + Thread.currentThread() + " i: " + i); i++;
}
System.out.println("Thread stop i="+ i);
}
};
testThread.start();
Thread.sleep(1000);
stop = true;
System.out.println("now, in main thread stop is: " + stop);
testThread.join();
}
}
The working thread is started to do increment on i and after one second, the flag is set as true in main thread. It is expected that we could see print out in working thread: “Thread stop i=”. Unfortunately it is NOT the case. Through process explorer we can find the working thread is still running:
The only way we can terminate it is to click this button in Eclipse:
The reason is: every thread in Java has its own thread local stack in runtime. Every time a thread tries to access the content of a variable, it first locates the variable content in the main memory, then loads this content from main memory to its local stack. Once this load is done, this relationship between thread local stack and main memory is cut.
Later, when thread performs modifications on the variable, the change is directly done on thread local stack. And at a given time ( scheduled by JVM, developer has no control about this timeslot), the change will be refreshed from thread local stack to memory. Back to our example, already the main thread has changed the flag to true in one second later ( this is TOO late! ), unfortunately when the working thread reads the flag from its own local stack, the flag is still false ( it makes sense since when it is copied from main memory in main thread ), so the working threads could never end. See the following picture for detail.
Solution is: add keyword volatile to flag variable, to force the read access on it in working thread is done always from main memory, so that after the flag is changed to true in main thread, later the working thread can detect this latest change since it reads data from main memory now. After this keyword is added we can get expected output:
要獲取更多Jerry的原創文章,請關注公眾號"汪子熙":
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/24475491/viewspace-2724091/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- SAP Cloud for Customer裡一個Promise的實際應用場合CloudPromise
- 代理IP的三個實際應用場景
- nodejs實際應用場景NodeJS
- 閉包實際場景應用
- 一個實際的例子學習 SAP BTP Java 應用的 @Before 註解使用方式Java
- 在實際應用中聯合體union的妙用
- 棧的實際應用
- JavaScript 原型的實際應用之實現一個 jQueryJavaScript原型jQuery
- 頂級實用乾貨——談談Java中的volatileJava
- Google Guava 在實際場景中的應用封裝GoGuava封裝
- 八個Docker的真實應用場景Docker
- 廣泛被應用的雲專線實際應用場景有哪些?——VecloudCloud
- 反向代理的實際應用
- Canvas的實際應用場景1-生成一張主題分享圖片Canvas
- Python 基礎 2-2 列表的實際應用場景Python
- [Elasticsearch] ES 的Mapping 設計在實際場景中應用ElasticsearchAPP
- 使用ABAP併發程式設計解決一個實際應用場景中的效能瓶頸問題程式設計
- 一個具體的例子學習Java volatile關鍵字Java
- Linux在實際中的應用Linux
- 動態代理的實際應用
- sklearn中的pipeline實際應用
- Map-Reduce 思想在 ABAP 程式設計中的一個實際應用案例程式設計
- 微控制器中volatile的應用
- redis實際應用-限流Redis
- 滲透測試工具多個應用場合介紹
- Java volatile 的測試(Java程式碼實戰-004)Java
- 用一個實際例子理解Dockervolume工作原理Docker
- Java 組合模式及其應用Java模式
- java----volatile, 用更低的代價替代同步Java
- 回車符號 ‘ ’ 的實際應用符號
- 觀察者模式的實際應用模式
- 一個輕APP場景應用-外掛APP
- volatile的記憶體語義與應用記憶體
- python應用場景有哪些?實際就業薪資如何?Python就業
- Java程式中的代理作用和應用場景及實現Java
- 一致性 Hash 演算法的實際應用演算法
- java中ThreadLocal的應用場景分析Javathread
- 用一個實際例子理解Docker volume工作原理Docker