Java Synchronized的使用詳解
1.為什么要使用synchronized
在并發(fā)編程中存在線程安全問(wèn)題,主要原因有:1.存在共享數(shù)據(jù) 2.多線程共同操作共享數(shù)據(jù)。關(guān)鍵字synchronized可以保證在同一時(shí)刻,只有一個(gè)線程可以執(zhí)行某個(gè)方法或某個(gè)代碼塊,同時(shí)synchronized可以保證一個(gè)線程的變化可見(可見性),即可以代替volatile。
2.實(shí)現(xiàn)原理
synchronized可以保證方法或者代碼塊在運(yùn)行時(shí),同一時(shí)刻只有一個(gè)方法可以進(jìn)入到臨界區(qū),同時(shí)它還可以保證共享變量的內(nèi)存可見性
3.synchronized的三種應(yīng)用方式
Java中每一個(gè)對(duì)象都可以作為鎖,這是synchronized實(shí)現(xiàn)同步的基礎(chǔ):
普通同步方法(實(shí)例方法),鎖是當(dāng)前實(shí)例對(duì)象 ,進(jìn)入同步代碼前要獲得當(dāng)前實(shí)例的鎖靜態(tài)同步方法,鎖是當(dāng)前類的class對(duì)象 ,進(jìn)入同步代碼前要獲得當(dāng)前類對(duì)象的鎖同步方法塊,鎖是括號(hào)里面的對(duì)象,對(duì)給定對(duì)象加鎖,進(jìn)入同步代碼庫(kù)前要獲得給定對(duì)象的鎖。
4.synchronized的作用
Synchronized是Java中解決并發(fā)問(wèn)題的一種最常用最簡(jiǎn)單的方法 ,他可以確保線程互斥的訪問(wèn)同步代碼
5.舉栗子
**一、synchronized作用于實(shí)例方法**
①多個(gè)線程訪問(wèn)同一個(gè)對(duì)象的同一個(gè)方法
public class synchronizedTest implements Runnable { //共享資源 static int i =0; /** * synchronized 修飾實(shí)例方法 */ public synchronized void increase(){ i++; } @Override public void run(){ for (int j =0 ; j<10000;j++){ increase(); } } public static void main(String[] args) throws InterruptedException { synchronizedTest test = new synchronizedTest(); Thread t1 = new Thread(test); Thread t2 = new Thread(test); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); }}
結(jié)果:
分析:當(dāng)兩個(gè)線程同時(shí)對(duì)一個(gè)對(duì)象的一個(gè)方法進(jìn)行操作,只有一個(gè)線程能夠搶到鎖。因?yàn)橐粋€(gè)對(duì)象只有一把鎖,一個(gè)線程獲取了該對(duì)象的鎖之后,其他線程無(wú)法獲取該對(duì)象的鎖,就不能訪問(wèn)該對(duì)象的其他synchronized實(shí)例方法,但是可以訪問(wèn)非synchronized修飾的方法
②一個(gè)線程獲取了該對(duì)象的鎖之后,其他線程來(lái)訪問(wèn)其他synchronized實(shí)例方法現(xiàn)象 舉栗
public class SynchronizedTest { public synchronized void method1() { System.out.println('Method 1 start'); try { System.out.println('Method 1 execute'); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('Method 1 end'); } public synchronized void method2() { System.out.println('Method 2 start'); try { System.out.println('Method 2 execute'); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('Method 2 end'); } public static void main(String[] args) { final SynchronizedTest test = new SynchronizedTest(); new Thread(test::method1).start(); new Thread(test::method2).start(); }}
結(jié)果:
分析:可以看出其他線程來(lái)訪問(wèn)synchronized修飾的其他方法時(shí)需要等待線程1先把鎖釋放
③一個(gè)線程獲取了該對(duì)象的鎖之后,其他線程來(lái)訪問(wèn)其他非synchronized實(shí)例方法現(xiàn)象 舉栗去掉②中方法二的synchronized
public class SynchronizedTest { public synchronized void method1() { System.out.println('Method 1 start'); try { System.out.println('Method 1 execute'); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('Method 1 end'); } public void method2() { System.out.println('Method 2 start'); try { System.out.println('Method 2 execute'); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('Method 2 end'); } public static void main(String[] args) { final SynchronizedTest test = new SynchronizedTest(); new Thread(test::method1).start(); new Thread(test::method2).start(); }}
結(jié)果:
分析:當(dāng)線程1還在執(zhí)行時(shí),線程2也執(zhí)行了,所以當(dāng)其他線程來(lái)訪問(wèn)非synchronized修飾的方法時(shí)是可以訪問(wèn)的
④當(dāng)多個(gè)線程作用于不同的對(duì)象
public class SynchronizedTest { public synchronized void method1() { System.out.println('Method 1 start'); try { System.out.println('Method 1 execute'); Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('Method 1 end'); } public synchronized void method2() { System.out.println('Method 2 start'); try { System.out.println('Method 2 execute'); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println('Method 2 end'); } public static void main(String[] args) { final SynchronizedTest test1 = new SynchronizedTest(); final SynchronizedTest test2 = new SynchronizedTest(); new Thread(test1::method1).start(); new Thread(test2::method2).start(); }}
結(jié)果:
分析:因?yàn)閮蓚€(gè)線程作用于不同的對(duì)象,獲得的是不同的鎖,所以互相并不影響
**此處思考一個(gè)問(wèn)題:為什么分布式環(huán)境下synchronized失效?如何解決這種情況?
****二、synchronized作用于靜態(tài)方法**
public class synchronizedTest implements Runnable { //共享資源 static int i =0; /** * synchronized 修飾實(shí)例方法 */ public static synchronized void increase(){ i++; } @Override public void run(){ for (int j =0 ; j<10000;j++){ increase(); } } public static void main(String[] args) throws InterruptedException { Thread t1 = new Thread(new synchronizedTest()); Thread t2 = new Thread(new synchronizedTest()); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); }
結(jié)果:
分析:由例子可知,兩個(gè)線程實(shí)例化兩個(gè)不同的對(duì)象,但是訪問(wèn)的方法是靜態(tài)的,兩個(gè)線程發(fā)生了互斥(即一個(gè)線程訪問(wèn),另一個(gè)線程只能等著),因?yàn)殪o態(tài)方法是依附于類而不是對(duì)象的,當(dāng)synchronized修飾靜態(tài)方法時(shí),鎖是class對(duì)象。
**三、synchronized作用于同步代碼塊**
為什么要同步代碼塊呢?在某些情況下,我們編寫的方法體可能比較大,同時(shí)存在一些比較耗時(shí)的操作,而需要同步的代碼又只有一小部分,如果直接對(duì)整個(gè)方法進(jìn)行同步操作,可能會(huì)得不償失,此時(shí)我們可以使用同步代碼塊的方式對(duì)需要同步的代碼進(jìn)行包裹,這樣就無(wú)需對(duì)整個(gè)方法進(jìn)行同步操作了。
public class synchronizedTest implements Runnable { static synchronizedTest instance=new synchronizedTest(); static int i=0; @Override public void run() { //省略其他耗時(shí)操作.... //使用同步代碼塊對(duì)變量i進(jìn)行同步操作,鎖對(duì)象為instance synchronized(instance){ for(int j=0;j<10000;j++){i++; } } } public static void main(String[] args) throws InterruptedException { Thread t1=new Thread(instance); Thread t2=new Thread(instance); t1.start(); t2.start(); t1.join(); t2.join(); System.out.println(i); }}
結(jié)果:
分析:將synchronized作用于一個(gè)給定的實(shí)例對(duì)象instance,即當(dāng)前實(shí)例對(duì)象就是鎖對(duì)象,每次當(dāng)線程進(jìn)入synchronized包裹的代碼塊時(shí)就會(huì)要求當(dāng)前線程持有instance實(shí)例對(duì)象鎖,如果當(dāng)前有其他線程正持有該對(duì)象鎖,那么新到的線程就必須等待,這樣也就保證了每次只有一個(gè)線程執(zhí)行i++;操作。當(dāng)然除了instance作為對(duì)象外,我們還可以使用this對(duì)象(代表當(dāng)前實(shí)例)或者當(dāng)前類的class對(duì)象作為鎖,如下代碼:
//this,當(dāng)前實(shí)例對(duì)象鎖synchronized(this){ for(int j=0;j<1000000;j++){ i++; }}//class對(duì)象鎖synchronized(AccountingSync.class){ for(int j=0;j<1000000;j++){ i++; }}
下一篇將深入介紹Synchronized的實(shí)現(xiàn)原理
到此這篇關(guān)于Java Synchronized的使用詳解的文章就介紹到這了,更多相關(guān)Java Synchronized使用內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究2. 三個(gè)不常見的 HTML5 實(shí)用新特性簡(jiǎn)介3. Angular獲取ngIf渲染的Dom元素示例4. IIS+PHP添加對(duì)webp格式圖像的支持配置方法5. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附j(luò)son.min.asp6. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)7. 使用.net core 自帶DI框架實(shí)現(xiàn)延遲加載功能8. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問(wèn)題分析9. php測(cè)試程序運(yùn)行速度和頁(yè)面執(zhí)行速度的代碼10. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過(guò)程解析
