在Java 5以前,是用synchronized關鍵字來實現鎖的功能。
synchronized關鍵字可以作為方法的修飾符(同步方法),也可作用于函數內的語句(同步代碼塊)。
掌握synchronized,關鍵是要掌握把那個東西作為鎖。對于類的非靜態方法(成員方法)而言,意味著要取得對象實例的鎖;對于類的靜態方法(類方法)而言,要取得類的Class對象的鎖;對于同步代碼塊,要指定取得的是哪個對象的鎖。同步非靜態方法可以視為包含整個方法的synchronized(this) { … }代碼塊。
不管是同步代碼塊還是同步方法,每次只有一個線程可以進入(在同一時刻最多只有一個線程執行該段代碼。),如果其他線程試圖進入(不管是同一同步塊還是不同的同步塊),jvm會將它們掛起(放入到等鎖池中)。這種結構在并發理論中稱為臨界區(critical section)。
在jvm內部,為了提高效率,同時運行的每個線程都會有它正在處理的數據的緩存副本,當我們使用synchronzied進行同步的時候,真正被同步的是在不同線程中表示被鎖定對象的內存塊(副本數據會保持和主內存的同步,現在知道為什么要用同步這個詞匯了吧),簡單的說就是在同步塊或同步方法執行完后,對被鎖定的對象做的任何修改要在釋放鎖之前寫回到主內存中;在進入同步塊得到鎖之后,被鎖定對象的數據是從主內存中讀出來的,持有鎖的線程的數據副本一定和主內存中的數據視圖是同步的 。
下面舉具體的例子來說明synchronized的各種情況。
synchronized同步方法
首先來看同步方法的例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
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(); } } |
運行該程序輸出結果為:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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方法修改為如下所示,則兩個線程并不能同步執行,因為此時兩個線程的同步監視器不是同一個對象,不能起到同步的作用。
1
2
3
4
5
6
7
8
|
public static void main(String[] args) { Thread t = new SynchronizedTest1(); t.start(); Thread t1 = new SynchronizedTest1(); t1.start(); } |
此時輸出結果如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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方法聲明為靜態方法,這樣兩個線程的監視器是同一個對象(類對象),能夠同步執行。修改后的代碼如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
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(); } } |
輸出結果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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對象控制同一對象實例之間的同步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
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(); } } |
輸出結果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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對象控制不同實例之間的同步:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
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(); } } |
輸出結果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
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實例
兩個線程同時訪問一個對象的同步方法
當兩個并發線程訪問同一個對象的同步方法時,只能有一個線程得到執行。另一個線程必須等待當前線程執行完這個以后才能執行。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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) { } } } } |
輸出結果:
1
2
3
4
5
6
7
8
9
10
|
A : 0 A : 1 A : 2 A : 3 A : 4 B : 0 B : 1 B : 2 B : 3 B : 4 |
兩個線程訪問的是兩個對象的同步方法
這種情況下,synchronized不起作用,跟普通的方法一樣。因為對應的鎖是各自的對象。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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) { } } } } |
其中一種可能的輸出結果:
1
2
3
4
5
6
7
8
9
10
|
Object2 : 0 Object1 : 0 Object1 : 1 Object2 : 1 Object2 : 2 Object1 : 2 Object2 : 3 Object1 : 3 Object1 : 4 Object2 : 4 |
兩個線程訪問的是synchronized的靜態方法
這種情況,由于鎖住的是Class,在任何時候,該靜態方法只有一個線程可以執行。
同時訪問同步方法與非同步方法
當一個線程訪問對象的一個同步方法時,另一個線程仍然可以訪問該對象中的非同步方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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) { } } } } |
一種可能的輸出結果:
1
2
3
4
5
6
7
8
9
10
|
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時,其他線程對該對象中所有其它同步方法的訪問將被阻塞。因為第一個線程已經獲得了對象鎖,其他線程得不到鎖,則雖然是訪問不同的方法,但是沒有獲得鎖,也無法訪問。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
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) { } } } } |
輸出結果:
1
2
3
4
5
6
7
8
9
10
|
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 |