synchronized 關(guān)鍵字,代表這個(gè)方法加鎖,相當(dāng)于不管哪一個(gè)線程(例如線程A),運(yùn)行到這個(gè)方法時(shí),都要檢查有沒有其它線程B(或者C、 D等)正在用這個(gè)方法(或者該類的其他同步方法),有的話要等正在使用synchronized方法的線程B(或者C 、D)運(yùn)行完這個(gè)方法后再運(yùn)行此線程A,沒有的話,鎖定調(diào)用者,然后直接運(yùn)行。它包括兩種用法:synchronized 方法和 synchronized 塊。
多線程的同步機(jī)制對(duì)資源進(jìn)行加鎖,使得在同一個(gè)時(shí)間,只有一個(gè)線程可以進(jìn)行操作,同步用以解決多個(gè)線程同時(shí)訪問時(shí)可能出現(xiàn)的問題。
同步機(jī)制可以使用synchronized關(guān)鍵字實(shí)現(xiàn)。
當(dāng)synchronized關(guān)鍵字修飾一個(gè)方法的時(shí)候,該方法叫做同步方法。
當(dāng)synchronized方法執(zhí)行完或發(fā)生異常時(shí),會(huì)自動(dòng)釋放鎖。
下面通過一個(gè)例子來對(duì)synchronized關(guān)鍵字的用法進(jìn)行解析。
1.是否使用synchronized關(guān)鍵字的不同
例子程序1
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
41
42
|
public class ThreadTest { public static void main(String[] args) { Example example = new Example(); Thread t1 = new Thread1(example); Thread t2 = new Thread1(example); t1.start(); t2.start(); } } class Example { public synchronized void execute() { for ( int i = 0 ; i < 10 ; ++i) { try { Thread.sleep( 500 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "Hello: " + i); } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { example.execute(); } } |
是否在execute()方法前加上synchronized關(guān)鍵字,這個(gè)例子程序的執(zhí)行結(jié)果會(huì)有很大的不同。
如果不加synchronized關(guān)鍵字,則兩個(gè)線程同時(shí)執(zhí)行execute()方法,輸出是兩組并發(fā)的。
如果加上synchronized關(guān)鍵字,則會(huì)先輸出一組0到9,然后再輸出下一組,說明兩個(gè)線程是順次執(zhí)行的。
2.多個(gè)方法的多線程情況
將程序改動(dòng)一下,Example類中再加入一個(gè)方法execute2()。
之后再寫一個(gè)線程類Thread2,Thread2中的run()方法執(zhí)行的是execute2()。Example類中的兩個(gè)方法都是被synchronized關(guān)鍵字修飾的。
例子程序2
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
public class ThreadTest { public static void main(String[] args) { Example example = new Example(); Thread t1 = new Thread1(example); Thread t2 = new Thread2(example); t1.start(); t2.start(); } } class Example { public synchronized void execute() { for ( int i = 0 ; i < 20 ; ++i) { try { Thread.sleep(( long ) Math.random() * 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "Hello: " + i); } } public synchronized void execute2() { for ( int i = 0 ; i < 20 ; ++i) { try { Thread.sleep(( long ) Math.random() * 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "World: " + i); } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { example.execute(); } } class Thread2 extends Thread { private Example example; public Thread2(Example example) { this .example = example; } @Override public void run() { example.execute2(); } } |
如果去掉synchronized關(guān)鍵字,則兩個(gè)方法并發(fā)執(zhí)行,并沒有相互影響。
但是如例子程序中所寫,即便是兩個(gè)方法:
執(zhí)行結(jié)果永遠(yuǎn)是執(zhí)行完一個(gè)線程的輸出再執(zhí)行另一個(gè)線程的。
說明:
如果一個(gè)對(duì)象有多個(gè)synchronized方法,某一時(shí)刻某個(gè)線程已經(jīng)進(jìn)入到了某個(gè)synchronized方法,那么在該方法沒有執(zhí)行完畢前,其他線程是無法訪問該對(duì)象的任何synchronized方法的。
結(jié)論:
當(dāng)synchronized關(guān)鍵字修飾一個(gè)方法的時(shí)候,該方法叫做同步方法。
Java中的每個(gè)對(duì)象都有一個(gè)鎖(lock),或者叫做監(jiān)視器(monitor),當(dāng)一個(gè)線程訪問某個(gè)對(duì)象的synchronized方法時(shí),將該對(duì)象上鎖,其他任何線程都無法再去訪問該對(duì)象的synchronized方法了(這里是指所有的同步方法,而不僅僅是同一個(gè)方法),直到之前的那個(gè)線程執(zhí)行方法完畢后(或者是拋出了異常),才將該對(duì)象的鎖釋放掉,其他線程才有可能再去訪問該對(duì)象的synchronized方法。
注意這時(shí)候是給對(duì)象上鎖,如果是不同的對(duì)象,則各個(gè)對(duì)象之間沒有限制關(guān)系。
嘗試在代碼中構(gòu)造第二個(gè)線程對(duì)象時(shí)傳入一個(gè)新的Example對(duì)象,則兩個(gè)線程的執(zhí)行之間沒有什么制約關(guān)系。
3.考慮靜態(tài)的同步方法
當(dāng)一個(gè)synchronized關(guān)鍵字修飾的方法同時(shí)又被static修飾,之前說過,非靜態(tài)的同步方法會(huì)將對(duì)象上鎖,但是靜態(tài)方法不屬于對(duì)象,而是屬于類,它會(huì)將這個(gè)方法所在的類的Class對(duì)象上鎖。
一個(gè)類不管生成多少個(gè)對(duì)象,它們所對(duì)應(yīng)的是同一個(gè)Class對(duì)象。
例子程序3
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
public class ThreadTest { public static void main(String[] args) { Example example = new Example(); Thread t1 = new Thread1(example); // 此處即便傳入不同的對(duì)象,靜態(tài)方法同步仍然不允許多個(gè)線程同時(shí)執(zhí)行 example = new Example(); Thread t2 = new Thread2(example); t1.start(); t2.start(); } } class Example { public synchronized static void execute() { for ( int i = 0 ; i < 20 ; ++i) { try { Thread.sleep(( long ) Math.random() * 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "Hello: " + i); } } public synchronized static void execute2() { for ( int i = 0 ; i < 20 ; ++i) { try { Thread.sleep(( long ) Math.random() * 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "World: " + i); } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { Example.execute(); } } class Thread2 extends Thread { private Example example; public Thread2(Example example) { this .example = example; } @Override public void run() { Example.execute2(); } } |
所以如果是靜態(tài)方法的情況(execute()和execute2()都加上static關(guān)鍵字),即便是向兩個(gè)線程傳入不同的Example對(duì)象,這兩個(gè)線程仍然是互相制約的,必須先執(zhí)行完一個(gè),再執(zhí)行下一個(gè)。
結(jié)論:
如果某個(gè)synchronized方法是static的,那么當(dāng)線程訪問該方法時(shí),它鎖的并不是synchronized方法所在的對(duì)象,而是synchronized方法所在的類所對(duì)應(yīng)的Class對(duì)象。Java中,無論一個(gè)類有多少個(gè)對(duì)象,這些對(duì)象會(huì)對(duì)應(yīng)唯一一個(gè)Class對(duì)象,因此當(dāng)線程分別訪問同一個(gè)類的兩個(gè)對(duì)象的兩個(gè)static,synchronized方法時(shí),它們的執(zhí)行順序也是順序的,也就是說一個(gè)線程先去執(zhí)行方法,執(zhí)行完畢后另一個(gè)線程才開始。
4. synchronized塊
synchronized塊寫法:
1
2
3
|
synchronized (object) { } |
表示線程在執(zhí)行的時(shí)候會(huì)將object對(duì)象上鎖。(注意這個(gè)對(duì)象可以是任意類的對(duì)象,也可以使用this關(guān)鍵字)。
這樣就可以自行規(guī)定上鎖對(duì)象。
例子程序4
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
public class ThreadTest { public static void main(String[] args) { Example example = new Example(); Thread t1 = new Thread1(example); Thread t2 = new Thread2(example); t1.start(); t2.start(); } } class Example { private Object object = new Object(); public void execute() { synchronized (object) { for ( int i = 0 ; i < 20 ; ++i) { try { Thread.sleep(( long ) Math.random() * 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "Hello: " + i); } } } public void execute2() { synchronized (object) { for ( int i = 0 ; i < 20 ; ++i) { try { Thread.sleep(( long ) Math.random() * 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "World: " + i); } } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { example.execute(); } } class Thread2 extends Thread { private Example example; public Thread2(Example example) { this .example = example; } @Override public void run() { example.execute2(); } } |
例子程序4所達(dá)到的效果和例子程序2的效果一樣,都是使得兩個(gè)線程的執(zhí)行順序進(jìn)行,而不是并發(fā)進(jìn)行,當(dāng)一個(gè)線程執(zhí)行時(shí),將object對(duì)象鎖住,另一個(gè)線程就不能執(zhí)行對(duì)應(yīng)的塊。
synchronized方法實(shí)際上等同于用一個(gè)synchronized塊包住方法中的所有語句,然后在synchronized塊的括號(hào)中傳入this關(guān)鍵字。當(dāng)然,如果是靜態(tài)方法,需要鎖定的則是class對(duì)象。
可能一個(gè)方法中只有幾行代碼會(huì)涉及到線程同步問題,所以synchronized塊比synchronized方法更加細(xì)粒度地控制了多個(gè)線程的訪問,只有synchronized塊中的內(nèi)容不能同時(shí)被多個(gè)線程所訪問,方法中的其他語句仍然可以同時(shí)被多個(gè)線程所訪問(包括synchronized塊之前的和之后的)。
注意:被synchronized保護(hù)的數(shù)據(jù)應(yīng)該是私有的。
結(jié)論:
synchronized方法是一種粗粒度的并發(fā)控制,某一時(shí)刻,只能有一個(gè)線程執(zhí)行該synchronized方法;
synchronized塊則是一種細(xì)粒度的并發(fā)控制,只會(huì)將塊中的代碼同步,位于方法內(nèi)、synchronized塊之外的其他代碼是可以被多個(gè)線程同時(shí)訪問到的。