摘要:的應(yīng)用方式代碼塊作用范圍在中,作用對象是調(diào)用這個代碼塊的對象。方法進來了出來了運行的結(jié)果如下等把方法執(zhí)行完,釋放了的鎖,才開始執(zhí)行。靜態(tài)方法運行的結(jié)果如下等待執(zhí)行完才執(zhí)行,說明是類鎖類所的另外一種形式運行結(jié)果如下
synchronized的應(yīng)用方式
代碼塊:作用范圍在{}中,作用對象是調(diào)用這個代碼塊的對象。
方法:作用范圍是一個方法,作用對象是調(diào)用這個方法的對象。
靜態(tài)方法:作用范圍是這個靜態(tài)方法,作用對象是這個類的所有對象。
1,2是對象鎖,3是類鎖
舉例 代碼塊 無thispublic class SynchronizeDemo1 { static String syn = new String(); static class SynClass { public void myRun() { try { System.out.println(Thread.currentThread().getName() + "進來了"); synchronized (syn) { Thread.sleep(3000); } System.out.println(Thread.currentThread().getName() + "出來了"); } catch (InterruptedException e) { e.printStackTrace(); } } } static class Runnable1 implements Runnable { SynClass synClass; public Runnable1(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun(); } } static class Runnable2 implements Runnable { SynClass synClass; public Runnable2(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun(); } } public static void main(String[] args) { SynClass synClass = new SynClass(); Runnable1 runnable1 = new Runnable1(synClass); Runnable2 runnable2 = new Runnable2(synClass); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } }
運行的結(jié)果如下:
等thread1把代碼塊的執(zhí)行完,釋放了syn的鎖,thread2才開始執(zhí)行。
有thispublic class SynchronizeDemo2 { static String syn = new String(); static class SynClass { public void myRun() { try { System.out.println(Thread.currentThread().getName() + "-myRun"); synchronized (this) { Thread.sleep(3000); } System.out.println(Thread.currentThread().getName() + "-myRun"); } catch (InterruptedException e) { e.printStackTrace(); } } public void myRun2() { try { System.out.println(Thread.currentThread().getName() + "-myRun2"); synchronized (this) { Thread.sleep(3000); } System.out.println(Thread.currentThread().getName() + "-myRun2"); } catch (InterruptedException e) { e.printStackTrace(); } } } static class Runnable1 implements Runnable { SynClass synClass; public Runnable1(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun(); } } static class Runnable2 implements Runnable { SynClass synClass; public Runnable2(SynClass synClass) { this.synClass = synClass; } @Override public void run() { synClass.myRun2(); } } public static void main(String[] args) { SynClass synClass = new SynClass(); Runnable1 runnable1 = new Runnable1(synClass); Runnable2 runnable2 = new Runnable2(synClass); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } }
運行的結(jié)果如下:
等thread1把代碼塊的執(zhí)行完,釋放了this的鎖,thread2才開始執(zhí)行。
public class SynchronizeDemo3 extends Thread { @Override public void run() { sync(); } synchronized public void sync(){ System.out.println(Thread.currentThread().getName() + "進來了"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "出來了"); } public static void main(String[] args) { SynchronizeDemo3 synchronizeDemo1 = new SynchronizeDemo3(); Thread thread1 = new Thread(synchronizeDemo1); thread1.setName("thread1"); Thread thread2 = new Thread(synchronizeDemo1); thread2.setName("thread2"); thread1.start(); thread2.start(); } }
運行的結(jié)果如下:
等thread1把方法執(zhí)行完,釋放了的鎖,thread2才開始執(zhí)行。
靜態(tài)方法public class SynchronizeDemo4 { static class Runnable1 implements Runnable { @Override public void run() { SynClass.myRun(); } } static class Runnable2 implements Runnable { @Override public void run() { SynClass.myRun2(); } } public static void main(String[] args) { Runnable1 runnable1 = new Runnable1(); Runnable2 runnable2 = new Runnable2(); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } } class SynClass { public synchronized static void myRun() { System.out.println(Thread.currentThread().getName() + "-myRun"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun"); } public synchronized static void myRun2() { System.out.println(Thread.currentThread().getName() + "-myRun2"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun2"); } }
運行的結(jié)果如下:
thread1等待thread2執(zhí)行完才執(zhí)行,說明是類鎖
類所的另外一種形式public class SynchronizeDemo5 { static class Runnable1 implements Runnable { @Override public void run() { synchronized (SynClass2.class){ System.out.println(Thread.currentThread().getName() + "-myRun"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun"); } } } static class Runnable2 implements Runnable { @Override public void run() { synchronized (SynClass2.class){ System.out.println(Thread.currentThread().getName() + "-myRun2"); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "-myRun2"); } } } public static void main(String[] args) { Runnable1 runnable1 = new Runnable1(); Runnable2 runnable2 = new Runnable2(); Thread thread1 = new Thread(runnable1); thread1.setName("thread1"); Thread thread2 = new Thread(runnable2); thread2.setName("thread2"); thread1.start(); thread2.start(); } } class SynClass2 { }
運行結(jié)果如下:
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/75140.html
摘要:線程安全問題在并發(fā)編程學(xué)習(xí)之基礎(chǔ)概念提到,多線程的劣勢之一,有個線程安全問題,現(xiàn)在看看下面的例子。那么,該怎么解決呢,很簡單,在方法前加個同步鎖。運行結(jié)果如下有兩種情況,是因為看誰先搶占鎖,但是輸出的算法結(jié)果是正確的。 線程安全問題 在java并發(fā)編程學(xué)習(xí)之基礎(chǔ)概念提到,多線程的劣勢之一,有個線程安全問題,現(xiàn)在看看下面的例子。 public class NotSafeDemo { ...
摘要:不釋放持有的鎖,釋放鎖。在調(diào)用方法前,必須持有鎖,調(diào)用喚醒,也要持有鎖。休眠一定時間后,進入就緒狀態(tài)。這兩個都能被方法中斷當(dāng)前狀態(tài)。用法方獲取鎖判斷條件,不滿足繼續(xù)滿足執(zhí)行其他業(yè)務(wù)方獲取鎖改變條件通知為什么是而不是會一直循環(huán),直到條件滿足。 sleep和wait sleep是Thread類的方法,wait是Object的方法。 sleep可以到處使用,wait必須是在同步方法或者代碼...
摘要:線程把的值放在中。線程執(zhí)行,得到的為。,,獨占鎖,會導(dǎo)致其他所有需要鎖的線程掛起,等待持有鎖的線程釋放鎖??梢娦裕槐WC原子性。樂觀鎖,不適用鎖的情況下實現(xiàn)多線程的變量同步。性能問題在并發(fā)量較高的情況下,如果一直不成功,會一直增加的開銷。 cas原理 cas全稱Compare and swap,比較和交換的意思。原子操作,需要硬件的支持。三個基本操作數(shù):內(nèi)存地址V,舊的預(yù)期值A(chǔ),要修改...
摘要:顯示鎖和內(nèi)置鎖內(nèi)置鎖優(yōu)勢代碼簡潔不會因為沒釋放鎖,導(dǎo)致鎖泄露。顯示鎖優(yōu)勢靈活性強,鎖的獲取可以被中斷,可以嘗試獲取鎖。接口接口主要方法如下獲取鎖嘗試獲取鎖,表示未加鎖的情況。會進行搶鎖操作,如果獲取不到鎖,也會進入阻塞隊列等到喚醒。 顯示鎖和內(nèi)置鎖 內(nèi)置鎖(Synchronized)優(yōu)勢 代碼簡潔 不會因為沒釋放鎖,導(dǎo)致鎖泄露。 顯示鎖(Lock)優(yōu)勢 靈活性強,鎖的獲取可以被中...
摘要:和是配套使用的,方法容易導(dǎo)致死鎖。方法不會保證線程的資源正常釋放方法給線程打個停止標(biāo)記,將線程的中斷狀態(tài)設(shè)置為,并沒有馬上強制中斷線程,線程是否中斷由線程自己決定。終結(jié)狀態(tài),還是返回。方法判斷當(dāng)前線程是否中斷,清除中斷標(biāo)志。 resume、suspend、stop resume和suspend是配套使用的,suspend方法容易導(dǎo)致死鎖。 stop方法不會保證線程的資源正常釋放 i...
閱讀 2715·2021-11-25 09:43
閱讀 2097·2021-11-24 09:39
閱讀 1989·2021-11-17 09:33
閱讀 2765·2021-09-27 14:11
閱讀 1874·2019-08-30 15:54
閱讀 3236·2019-08-26 18:27
閱讀 1273·2019-08-23 18:00
閱讀 1821·2019-08-23 17:53