Java Synchronize下的volatile關(guān)鍵字詳解
簡介關(guān)鍵詞:Synchronize與volatile
- Synchronize:無論是對于Synchronize同步方法異或是Synchronize塊,本質(zhì)是對某對象或某類加鎖,讓多線程進(jìn)行隊列化的有序地同步執(zhí)行。
- volatile:用于修飾變量。在多線程執(zhí)行過程中,禁止線程從工作內(nèi)存(緩存)中讀取值。
volatile問題拋出:
讓我們看到這樣一個問題,我們設(shè)置一個含有boolean標(biāo)志位的類Test,以及兩個Runable接口實例,分別為MyThread1,MyThread2。
在MyThread1中通過while循環(huán)判斷flag是否更改,如果更改便結(jié)束循環(huán)退出。
在MyThread2中改變flag值。
代碼如下:
Test:
public class Test { boolean flag = true; }
MyThread1:
public class MyThread1 implements Runnable{ Test test; public MyThread1(Test test){ this.test = test; } @Override public void run() { while (test.flag){ } System.out.println(Thread.currentThread().getName()+" 我已退出"); } }
MyThread2:
public class MyThread2 implements Runnable{ Test test; public MyThread2(Test test){ this.test = test; } @Override public void run() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } test.flag = false; } }
main函數(shù):
public static void main(String[] args) { Test test = new Test(); MyThread1 myThread1 = new MyThread1(test); MyThread2 myThread2 = new MyThread2(test); Thread thread1 = new Thread(myThread1); Thread thread2 = new Thread(myThread2); thread1.start(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } thread2.start(); }
- 按照我們常規(guī)的想法,在在Sleep延時之后,Thread2會更改flag的值。而Thread1也會因此退出循環(huán)。
- 但實際上,Thread1并沒有因此退出循環(huán)。
- 原因是Thread1并未從內(nèi)存中讀取flag,而是直接從工作內(nèi)存中讀取。所以即便是Thread2已經(jīng)更新了flag的值,但Thread1工作內(nèi)存中的flag也并未更新。所以便導(dǎo)致了Thread1陷入死循環(huán)。
解決方法:
那么如何解決這樣的問題呢?
很簡單,使用volatile關(guān)鍵字。讓線程不得不從主內(nèi)存中讀取flag值。
volatile boolean flag = true;
在我們添加volatile關(guān)鍵字后,Thread1便可以正常退出。
在Synchronize下的volatile:
此時我們已經(jīng)了解了volatile關(guān)鍵字的作用,那么在我們的volatile關(guān)鍵字中,Synchronize有著怎樣的作用呢?
volatile問題拋出:
其實在我們實際使用中,volatile其實也是有一些隱患的。
例如:我們創(chuàng)造10條線程,每條線程都使volatile修飾的int常量增加1000000次。
public class MyThread1 implements Runnable{ volatile int num = 0; @Override public void run() { for (int i = 0; i < 1000000; i++) { num++; System.out.println(Thread.currentThread().getName()+" "+num); } } public static void main(String[] args) { MyThread1 myThread1 = new MyThread1(); Thread[] arr = new Thread[10]; for (int i = 0; i < 10; i++) { arr[i] = new Thread(myThread1); } for (int i = 0; i < 10; i++) { arr[i].start(); } } }
分析:
- 從結(jié)果中,我們可以看到,num并沒有像我們想象一樣達(dá)到10000000。
- 這是因為volatile所修飾的int變量在自加過程中并非原子操作。這也就是說這個自加的過程可以被打斷??梢员环纸鉃椋韩@取值,自加,賦值三個步驟。
- 例如當(dāng),num = 0時,Thread1獲取了num的值,并賦值為1,但此時在Thread1還未來得及更新線程的時候,Thread的2以及Thread3已經(jīng)將線程的值更新為2,但Thread1再賦值,num的值又會重新變?yōu)?。
- 所以,我們便需要在自加的過程中添加Synchronize關(guān)鍵字,讓線程實現(xiàn)同步。
結(jié)論:
在我們使用volatile關(guān)鍵字時,需要注意操作是否為原子操作,以免造成線程不安全。
擴展:
其實,對于原子操作,Java已經(jīng)提供了Atomic原子類來解決。其中涉及了CAS機制,在不使用Synchronize的情況下,通過比較原值與當(dāng)前值,不但性能高效,并且也能達(dá)到線程安全的目的。
到此這篇關(guān)于Synchronize下的volatile關(guān)鍵字 的文章就介紹到這了,更多相關(guān)Synchronize volatile關(guān)鍵字 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決SpringBoot項目啟動后網(wǎng)頁顯示Please sign in的問題
這篇文章主要介紹了解決SpringBoot項目啟動后網(wǎng)頁顯示Please sign in的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04Java之Swagger配置掃描接口以及開關(guān)案例講解
這篇文章主要介紹了Java之Swagger配置掃描接口以及開關(guān)案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08最流行的java后臺框架spring quartz定時任務(wù)
近日項目開發(fā)中需要執(zhí)行一些定時任務(wù),比如需要在每天凌晨時候,分析一次前一天的日志信息,借此機會整理了一下定時任務(wù)的幾種實現(xiàn)方式,由于項目采用spring框架,所以我都將結(jié)合spring框架來介紹2015-12-12springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼
這篇文章主要介紹了springboot+thymeleaf 文件上傳功能的實現(xiàn)代碼,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-11-11BootStrap Jstree 樹形菜單的增刪改查的實現(xiàn)源碼
這篇文章主要介紹了BootStrap Jstree 樹形菜單的增刪改查的實現(xiàn)源碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02