Java多線程法式中synchronized潤飾辦法的應用實例。本站提示廣大學習愛好者:(Java多線程法式中synchronized潤飾辦法的應用實例)文章只能為提供參考,不一定能成為您想要的結果。以下是Java多線程法式中synchronized潤飾辦法的應用實例正文
在Java 5之前,是用synchronized症結字來完成鎖的功效。
synchronized症結字可以作為辦法的潤飾符(同步辦法),也可感化於函數內的語句(同步代碼塊)。
控制synchronized,症結是要控制把誰人器械作為鎖。關於類的非靜態辦法(成員辦法)而言,意味著要獲得對象實例的鎖;關於類的靜態辦法(類辦法)而言,要獲得類的Class對象的鎖;關於同步代碼塊,要指定獲得的是哪一個對象的鎖。同步非靜態辦法可以視為包括全部辦法的synchronized(this) { … }代碼塊。
不論是同步代碼塊照樣同步辦法,每次只要一個線程可以進入(在統一時辰最多只要一個線程履行該段代碼。),假如其他線程試圖進入(不論是統一同步塊照樣分歧的同步塊),jvm會將它們掛起(放入到等鎖池中)。這類構造在並發實際中稱為臨界區(critical section)。
在jvm外部,為了進步效力,同時運轉的每一個線程都邑有它正在處置的數據的緩存正本,當我們應用synchronzied停止同步的時刻,真正被同步的是在分歧線程中表現被鎖定對象的內存塊(正本數據會堅持和主內存的同步,如今曉得為何要用同步這個辭匯了吧),簡略的說就是在同步塊或同步辦法履行完後,對被鎖定的對象做的任何修正要在釋放鎖之前寫回到主內存中;在進入同步塊獲得鎖以後,被鎖定對象的數據是從主內存中讀出來的,持有鎖的線程的數據正本必定和主內存中的數據視圖是同步的 。
上面舉詳細的例子來講明synchronized的各類情形。
synchronized同步辦法
起首來看同步辦法的例子:
public class SynchronizedTest1 extends Thread { private synchronized void testSynchronizedMethod() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " testSynchronizedMethod:" + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void run() { testSynchronizedMethod(); } public static void main(String[] args) { SynchronizedTest1 t = new SynchronizedTest1(); t.start(); t.testSynchronizedMethod(); } }
運轉該法式輸入成果為:
main testSynchronizedMethod:0 main testSynchronizedMethod:1 main testSynchronizedMethod:2 main testSynchronizedMethod:3 main testSynchronizedMethod:4 main testSynchronizedMethod:5 main testSynchronizedMethod:6 main testSynchronizedMethod:7 main testSynchronizedMethod:8 main testSynchronizedMethod:9 Thread-0 testSynchronizedMethod:0 Thread-0 testSynchronizedMethod:1 Thread-0 testSynchronizedMethod:2 Thread-0 testSynchronizedMethod:3 Thread-0 testSynchronizedMethod:4 Thread-0 testSynchronizedMethod:5 Thread-0 testSynchronizedMethod:6 Thread-0 testSynchronizedMethod:7 Thread-0 testSynchronizedMethod:8 Thread-0 testSynchronizedMethod:9
可以看到testSynchronizedMethod辦法在兩個線程之間同步履行。
假如此時將main辦法修正為以下所示,則兩個線程其實不能同步履行,由於此時兩個線程的同步監督器不是統一個對象,不克不及起到同步的感化。
public static void main(String[] args) { Thread t = new SynchronizedTest1(); t.start(); Thread t1 = new SynchronizedTest1(); t1.start(); }
此時輸入成果以下所示:
Thread-0 testSynchronizedMethod:0 Thread-1 testSynchronizedMethod:0 Thread-0 testSynchronizedMethod:1 Thread-1 testSynchronizedMethod:1 Thread-0 testSynchronizedMethod:2 Thread-1 testSynchronizedMethod:2 Thread-0 testSynchronizedMethod:3 Thread-1 testSynchronizedMethod:3 Thread-0 testSynchronizedMethod:4 Thread-1 testSynchronizedMethod:4 Thread-0 testSynchronizedMethod:5 Thread-1 testSynchronizedMethod:5 Thread-0 testSynchronizedMethod:6 Thread-1 testSynchronizedMethod:6 Thread-0 testSynchronizedMethod:7 Thread-1 testSynchronizedMethod:7 Thread-0 testSynchronizedMethod:8 Thread-1 testSynchronizedMethod:8 Thread-0 testSynchronizedMethod:9 Thread-1 testSynchronizedMethod:9
若想修正後的main辦法可以或許在兩個線程之間同步運轉,須要將testSynchronizedMethod辦法聲明為靜態辦法,如許兩個線程的監督器是統一個對象(類對象),可以或許同步履行。修正後的代碼以下所示:
public class SynchronizedTest1 extends Thread { private static synchronized void testSynchronizedMethod() { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " testSynchronizedMethod:" + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } @Override public void run() { testSynchronizedMethod(); } public static void main(String[] args) { Thread t = new SynchronizedTest1(); t.start(); Thread t1 = new SynchronizedTest1(); t1.start(); } }
輸入成果以下:
Thread-0 testSynchronizedMethod:0 Thread-0 testSynchronizedMethod:1 Thread-0 testSynchronizedMethod:2 Thread-0 testSynchronizedMethod:3 Thread-0 testSynchronizedMethod:4 Thread-0 testSynchronizedMethod:5 Thread-0 testSynchronizedMethod:6 Thread-0 testSynchronizedMethod:7 Thread-0 testSynchronizedMethod:8 Thread-0 testSynchronizedMethod:9 Thread-1 testSynchronizedMethod:0 Thread-1 testSynchronizedMethod:1 Thread-1 testSynchronizedMethod:2 Thread-1 testSynchronizedMethod:3 Thread-1 testSynchronizedMethod:4 Thread-1 testSynchronizedMethod:5 Thread-1 testSynchronizedMethod:6 Thread-1 testSynchronizedMethod:7 Thread-1 testSynchronizedMethod:8 Thread-1 testSynchronizedMethod:9
同步塊的情形與同步辦法相似,只是同步塊將同步掌握的粒度減少,如許可以或許更好的施展多線程並行履行的效力。
應用this對象掌握統一對象實例之間的同步:
public class SynchronizedTest2 extends Thread { private void testSynchronizedBlock() { synchronized (this) { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " testSynchronizedBlock:" + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } @Override public void run() { testSynchronizedBlock(); } public static void main(String[] args) { SynchronizedTest2 t = new SynchronizedTest2(); t.start(); t.testSynchronizedBlock(); } }
輸入成果:
main testSynchronizedBlock:0 main testSynchronizedBlock:1 main testSynchronizedBlock:2 main testSynchronizedBlock:3 main testSynchronizedBlock:4 main testSynchronizedBlock:5 main testSynchronizedBlock:6 main testSynchronizedBlock:7 main testSynchronizedBlock:8 main testSynchronizedBlock:9 Thread-0 testSynchronizedBlock:0 Thread-0 testSynchronizedBlock:1 Thread-0 testSynchronizedBlock:2 Thread-0 testSynchronizedBlock:3 Thread-0 testSynchronizedBlock:4 Thread-0 testSynchronizedBlock:5 Thread-0 testSynchronizedBlock:6 Thread-0 testSynchronizedBlock:7 Thread-0 testSynchronizedBlock:8 Thread-0 testSynchronizedBlock:9
應用class對象掌握分歧實例之間的同步:
public class SynchronizedTest2 extends Thread { private void testSynchronizedBlock() { synchronized (SynchronizedTest2.class) { for (int i = 0; i < 10; i++) { System.out.println(Thread.currentThread().getName() + " testSynchronizedBlock:" + i); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } } @Override public void run() { testSynchronizedBlock(); } public static void main(String[] args) { Thread t = new SynchronizedTest2(); t.start(); Thread t2 = new SynchronizedTest2(); t2.start(); } }
輸入成果:
Thread-0 testSynchronizedBlock:0 Thread-0 testSynchronizedBlock:1 Thread-0 testSynchronizedBlock:2 Thread-0 testSynchronizedBlock:3 Thread-0 testSynchronizedBlock:4 Thread-0 testSynchronizedBlock:5 Thread-0 testSynchronizedBlock:6 Thread-0 testSynchronizedBlock:7 Thread-0 testSynchronizedBlock:8 Thread-0 testSynchronizedBlock:9 Thread-1 testSynchronizedBlock:0 Thread-1 testSynchronizedBlock:1 Thread-1 testSynchronizedBlock:2 Thread-1 testSynchronizedBlock:3 Thread-1 testSynchronizedBlock:4 Thread-1 testSynchronizedBlock:5 Thread-1 testSynchronizedBlock:6 Thread-1 testSynchronizedBlock:7 Thread-1 testSynchronizedBlock:8 Thread-1 testSynchronizedBlock:9
應用synchronized症結字停止同步掌握時,必定要掌握好對象監督器,只要取得監督器的過程可以運轉,其它都須要期待獲得監督器。任何一個非null的對象都可以作為對象監督器,當synchronized感化在辦法上時,鎖住的就是對象實例(this);看成用在靜態辦法時鎖住的就是對象對應的Class實例
兩個線程同時拜訪一個對象的同步辦法
當兩個並發線程拜訪統一個對象的同步辦法時,只能有一個線程獲得履行。另外一個線程必需期待以後線程履行完這個今後能力履行。
public class TwoThread { public static void main(String[] args) { final TwoThread twoThread = new TwoThread(); Thread t1 = new Thread(new Runnable() { public void run() { twoThread.syncMethod(); } }, "A"); Thread t2 = new Thread(new Runnable() { public void run() { twoThread.syncMethod(); } }, "B"); t1.start(); t2.start(); } public synchronized void syncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
輸入成果:
A : 0 A : 1 A : 2 A : 3 A : 4 B : 0 B : 1 B : 2 B : 3 B : 4
兩個線程拜訪的是兩個對象的同步辦法
這類情形下,synchronized不起感化,跟通俗的辦法一樣。由於對應的鎖是各自的對象。
public class TwoObject { public static void main(String[] args) { final TwoObject object1 = new TwoObject(); Thread t1 = new Thread(new Runnable() { public void run() { object1.syncMethod(); } }, "Object1"); t1.start(); final TwoObject object2 = new TwoObject(); Thread t2 = new Thread(new Runnable() { public void run() { object2.syncMethod(); } }, "Object2"); t2.start(); } public synchronized void syncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " : " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
個中一種能夠的輸入成果:
Object2 : 0 Object1 : 0 Object1 : 1 Object2 : 1 Object2 : 2 Object1 : 2 Object2 : 3 Object1 : 3 Object1 : 4 Object2 : 4
兩個線程拜訪的是synchronized的靜態辦法
這類情形,因為鎖住的是Class,在任什麼時候候,該靜態辦法只要一個線程可以履行。
同時拜訪同步辦法與非同步辦法
當一個線程拜訪對象的一個同步辦法時,另外一個線程依然可以拜訪該對象中的非同步辦法。
public class SyncAndNoSync { public static void main(String[] args) { final SyncAndNoSync syncAndNoSync = new SyncAndNoSync(); Thread t1 = new Thread(new Runnable() { public void run() { syncAndNoSync.syncMethod(); } }, "A"); t1.start(); Thread t2 = new Thread(new Runnable() { public void run() { syncAndNoSync.noSyncMethod(); } }, "B"); t2.start(); } public synchronized void syncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at syncMethod(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } public void noSyncMethod() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at noSyncMethod(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
一種能夠的輸入成果:
B at noSyncMethod(): 0 A at syncMethod(): 0 B at noSyncMethod(): 1 A at syncMethod(): 1 B at noSyncMethod(): 2 A at syncMethod(): 2 B at noSyncMethod(): 3 A at syncMethod(): 3 A at syncMethod(): 4 B at noSyncMethod(): 4
拜訪統一個對象的分歧同步辦法
當一個線程拜訪一個對象的同步辦法A時,其他線程對該對象中一切其它同步辦法的拜訪將被壅塞。由於第一個線程曾經取得了對象鎖,其他線程得不到鎖,則固然是拜訪分歧的辦法,然則沒有取得鎖,也沒法拜訪。
public class TwoSyncMethod { public static void main(String[] args) { final TwoSyncMethod twoSyncMethod = new TwoSyncMethod(); Thread t1 = new Thread(new Runnable() { public void run() { twoSyncMethod.syncMethod1(); } }, "A"); t1.start(); Thread t2 = new Thread(new Runnable() { public void run() { twoSyncMethod.syncMethod2(); } }, "B"); t2.start(); } public synchronized void syncMethod1() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at syncMethod1(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } public synchronized void syncMethod2() { for (int i = 0; i < 5; i++) { System.out.println(Thread.currentThread().getName() + " at syncMethod2(): " + i); try { Thread.sleep(500); } catch (InterruptedException ie) { } } } }
輸入成果:
A at syncMethod1(): 0 A at syncMethod1(): 1 A at syncMethod1(): 2 A at syncMethod1(): 3 A at syncMethod1(): 4 B at syncMethod2(): 0 B at syncMethod2(): 1 B at syncMethod2(): 2 B at syncMethod2(): 3 B at syncMethod2(): 4