摘要:的應用方式代碼塊作用范圍在中,作用對象是調用這個代碼塊的對象。方法進來了出來了運行的結果如下等把方法執行完,釋放了的鎖,才開始執行。靜態方法運行的結果如下等待執行完才執行,說明是類鎖類所的另外一種形式運行結果如下
synchronized的應用方式
代碼塊:作用范圍在{}中,作用對象是調用這個代碼塊的對象。
方法:作用范圍是一個方法,作用對象是調用這個方法的對象。
靜態方法:作用范圍是這個靜態方法,作用對象是這個類的所有對象。
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(); } }
運行的結果如下:
等thread1把代碼塊的執行完,釋放了syn的鎖,thread2才開始執行。
有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(); } }
運行的結果如下:
等thread1把代碼塊的執行完,釋放了this的鎖,thread2才開始執行。
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(); } }
運行的結果如下:
等thread1把方法執行完,釋放了的鎖,thread2才開始執行。
靜態方法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"); } }
運行的結果如下:
thread1等待thread2執行完才執行,說明是類鎖
類所的另外一種形式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 { }
運行結果如下:
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://m.specialneedsforspecialkids.com/yun/75140.html
摘要:線程安全問題在并發編程學習之基礎概念提到,多線程的劣勢之一,有個線程安全問題,現在看看下面的例子。那么,該怎么解決呢,很簡單,在方法前加個同步鎖。運行結果如下有兩種情況,是因為看誰先搶占鎖,但是輸出的算法結果是正確的。 線程安全問題 在java并發編程學習之基礎概念提到,多線程的劣勢之一,有個線程安全問題,現在看看下面的例子。 public class NotSafeDemo { ...
摘要:不釋放持有的鎖,釋放鎖。在調用方法前,必須持有鎖,調用喚醒,也要持有鎖。休眠一定時間后,進入就緒狀態。這兩個都能被方法中斷當前狀態。用法方獲取鎖判斷條件,不滿足繼續滿足執行其他業務方獲取鎖改變條件通知為什么是而不是會一直循環,直到條件滿足。 sleep和wait sleep是Thread類的方法,wait是Object的方法。 sleep可以到處使用,wait必須是在同步方法或者代碼...
摘要:線程把的值放在中。線程執行,得到的為。,,獨占鎖,會導致其他所有需要鎖的線程掛起,等待持有鎖的線程釋放鎖。可見性,不保證原子性。樂觀鎖,不適用鎖的情況下實現多線程的變量同步。性能問題在并發量較高的情況下,如果一直不成功,會一直增加的開銷。 cas原理 cas全稱Compare and swap,比較和交換的意思。原子操作,需要硬件的支持。三個基本操作數:內存地址V,舊的預期值A,要修改...
摘要:顯示鎖和內置鎖內置鎖優勢代碼簡潔不會因為沒釋放鎖,導致鎖泄露。顯示鎖優勢靈活性強,鎖的獲取可以被中斷,可以嘗試獲取鎖。接口接口主要方法如下獲取鎖嘗試獲取鎖,表示未加鎖的情況。會進行搶鎖操作,如果獲取不到鎖,也會進入阻塞隊列等到喚醒。 顯示鎖和內置鎖 內置鎖(Synchronized)優勢 代碼簡潔 不會因為沒釋放鎖,導致鎖泄露。 顯示鎖(Lock)優勢 靈活性強,鎖的獲取可以被中...
摘要:和是配套使用的,方法容易導致死鎖。方法不會保證線程的資源正常釋放方法給線程打個停止標記,將線程的中斷狀態設置為,并沒有馬上強制中斷線程,線程是否中斷由線程自己決定。終結狀態,還是返回。方法判斷當前線程是否中斷,清除中斷標志。 resume、suspend、stop resume和suspend是配套使用的,suspend方法容易導致死鎖。 stop方法不會保證線程的資源正常釋放 i...
閱讀 2711·2021-11-25 09:43
閱讀 2093·2021-11-24 09:39
閱讀 1981·2021-11-17 09:33
閱讀 2763·2021-09-27 14:11
閱讀 1863·2019-08-30 15:54
閱讀 3233·2019-08-26 18:27
閱讀 1270·2019-08-23 18:00
閱讀 1818·2019-08-23 17:53