文章分享了4個例子對synchronized的詳細解釋
1、是否加synchronized關(guān)鍵字的不同
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
|
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 excute() { for ( int i = 0 ; i < 5 ; ++i) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "excute:" + i); } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { example.excute(); } } |
加了synchronized關(guān)鍵字的輸出結(jié)果如下
會先輸出一組0-4,接著再輸出下一組,兩個線程順序執(zhí)行
excute:0
excute:1
excute:2
excute:3
excute:4
excute:0
excute:1
excute:2
excute:3
excute:4
沒加synchronized關(guān)鍵字的輸出結(jié)果如下
兩個線程同時執(zhí)行excute方法,同時并發(fā)的
excute:0
excute:0
excute:1
excute:1
excute:2
excute:2
excute:3
excute:3
excute:4
excute:4
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
|
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 excute() { for ( int i = 0 ; i < 5 ; ++i) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "excute:" + i); } } public synchronized void excute1() { for ( int i = 0 ; i < 5 ; ++i) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "excute1:" + i); } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { example.excute(); } } class Thread2 extends Thread { private Example example; public Thread2(Example example) { this .example = example; } @Override public void run() { example.excute1(); } } |
執(zhí)行結(jié)果如下
同樣是順序執(zhí)行,執(zhí)行完一個線程再執(zhí)行另一個線程
excute:0
excute:1
excute:2
excute:3
excute:4
excute1:0
excute1:1
excute1:2
excute1:3
excute1:4
如果去掉synchronized關(guān)鍵字,則兩個方法并發(fā)執(zhí)行,并沒有相互影響。
但是如例子程序中所寫,即便是兩個方法:
執(zhí)行結(jié)果永遠是執(zhí)行完一個線程的輸出再執(zhí)行另一個線程的。
說明:
如果一個對象有多個synchronized方法,某一時刻某個線程已經(jīng)進入到了某個synchronized方法,那么在該方法沒有執(zhí)行完畢前,其他線程是無法訪問該對象的任何synchronized方法的。
結(jié)論:
當synchronized關(guān)鍵字修飾一個方法的時候,該方法叫做同步方法。
Java中的每個對象都有一個鎖(lock),或者叫做監(jiān)視器(monitor),當一個線程訪問某個對象的synchronized方法時,將該對象上鎖,其他任何線程都無法再去訪問該對象的synchronized方法了(這里是指所有的同步方法,而不僅僅是同一個方法),直到之前的那個線程執(zhí)行方法完畢后(或者是拋出了異常),才將該對象的鎖釋放掉,其他線程才有可能再去訪問該對象的synchronized方法。
注意這時候是給對象上鎖,如果是不同的對象,則各個對象之間沒有限制關(guān)系。
嘗試在代碼中構(gòu)造第二個線程對象時傳入一個新的Example對象,則兩個線程的執(zhí)行之間沒有什么制約關(guān)系。
3、靜態(tài)的同步方法
當一個synchronized關(guān)鍵字修飾的方法同時又被static修飾,之前說過,非靜態(tài)的同步方法會將對象上鎖,但是靜態(tài)方法不屬于對象,而是屬于類,它會將這個方法所在的類的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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
public class ThreadTest { public static void main(String[] args) { Example example = new Example(); Example example2 = new Example(); Thread t1 = new Thread1(example); Thread t2 = new Thread2(example2); t1.start(); t2.start(); } } class Example { public synchronized static void excute() { for ( int i = 0 ; i < 5 ; ++i) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "excute:" + i); } } public synchronized static void excute1() { for ( int i = 0 ; i < 5 ; ++i) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "excute1:" + i); } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { example.excute(); } } class Thread2 extends Thread { private Example example; public Thread2(Example example) { this .example = example; } @Override public void run() { example.excute1(); } } |
執(zhí)行結(jié)果如下
excute:0
excute:1
excute:2
excute:3
excute:4
excute1:0
excute1:1
excute1:2
excute1:3
excute1:4
如果沒有static修飾符,兩個線程分別傳入不同的對象,則會同時并發(fā)執(zhí)行
所以如果是靜態(tài)方法的情況(execute()和execute2()都加上static關(guān)鍵字),即便是向兩個線程傳入不同的Example對象,這兩個線程仍然是互相制約的,必須先執(zhí)行完一個,再執(zhí)行下一個。
結(jié)論:
如果某個synchronized方法是static的,那么當線程訪問該方法時,它鎖的并不是synchronized方法所在的對象,而是synchronized方法所在的類所對應(yīng)的Class對象。Java中,無論一個類有多少個對象,這些對象會對應(yīng)唯一一個Class對象,因此當線程分別訪問同一個類的兩個對象的兩個static,synchronized方法時,它們的執(zhí)行順序也是順序的,也就是說一個線程先去執(zhí)行方法,執(zhí)行完畢后另一個線程才開始。
4.synchronized塊
synchronized(object)
{
}
表示線程在執(zhí)行的時候會將object對象上鎖。(注意這個對象可以是任意類的對象,也可以使用this關(guān)鍵字)。
這樣就可以自行規(guī)定上鎖對象。
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
|
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 void excute() { synchronized ( this ) { for ( int i = 0 ; i < 5 ; ++i) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "excute:" + i); } } } public void excute1() { synchronized ( this ) { for ( int i = 0 ; i < 5 ; ++i) { try { Thread.sleep( 1000 ); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println( "excute1:" + i); } } } } class Thread1 extends Thread { private Example example; public Thread1(Example example) { this .example = example; } @Override public void run() { example.excute(); } } class Thread2 extends Thread { private Example example; public Thread2(Example example) { this .example = example; } @Override public void run() { example.excute1(); } } |
執(zhí)行結(jié)果如下
excute:0
excute:1
excute:2
excute:3
excute:4
excute1:0
excute1:1
excute1:2
excute1:3
excute1:4
例子程序4所達到的效果和例子程序2的效果一樣,都是使得兩個線程的執(zhí)行順序進行,而不是并發(fā)進行,當一個線程執(zhí)行時,將object對象鎖住,另一個線程就不能執(zhí)行對應(yīng)的塊。
synchronized方法實際上等同于用一個synchronized塊包住方法中的所有語句,然后在synchronized塊的括號中傳入this關(guān)鍵字。當然,如果是靜態(tài)方法,需要鎖定的則是class對象。
可能一個方法中只有幾行代碼會涉及到線程同步問題,所以synchronized塊比synchronized方法更加細粒度地控制了多個線程的訪問,只有synchronized塊中的內(nèi)容不能同時被多個線程所訪問,方法中的其他語句仍然可以同時被多個線程所訪問(包括synchronized塊之前的和之后的)。
結(jié)論:
synchronized方法是一種粗粒度的并發(fā)控制,某一時刻,只能有一個線程執(zhí)行該synchronized方法;
synchronized塊則是一種細粒度的并發(fā)控制,只會將塊中的代碼同步,位于方法內(nèi)、synchronized塊之外的其他代碼是可以被多個線程同時訪問到的。
以上就是關(guān)于java多線程編程Synchronized塊同步方法,希望對大家的學(xué)習(xí)有所幫助。