java synchronized 詳解
synchronized關鍵字有兩種用法,一種是只用于方法的定義中,另外一種是synchronized塊,我們不僅可以使用synchronized來同步一個對象變量,你也可以通synchronizedl來同步類中的靜態方法和非靜態方法。
synchronized塊的語法如下:
1
2
3
4
5
6
7
8
|
public void method() { synchronized (表達式) { } } <br> |
1
2
3
4
5
6
7
|
public void method() { synchronized (表達式) { } } |
第一種:非靜態方法的同步
從Java相關語法可以知道使用synchronized關鍵字來定義方法就會鎖定類中所用使用synchroniezd關鍵字定義的靜態方法和非靜態方法,但是這有點不好理解,如果要synchronized塊,來達到這樣的效果,就不難理解為什么會產生這種效果了,如果使用synchronized來鎖定類中所有的同步非靜態方法,只需要使用this作為synchronized塊的參數傳入synchronized塊中,代碼如下:
通過synchronized塊來同步非靜態方法
在上面的代碼中的method1使用了synchronized塊,method2方法是用了synchronized關鍵字來定義方法,如果使用同一個Test實例時,這兩個方法只要有一個在執行,其他的方法都會因未獲得同步鎖而被堵塞。除了使用this作為synchronized塊的參數,也可以使用Test.this作為synchronized塊的參數來達到同樣的效果。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class Test { public void method1() { synchronized ( this ) { } } public synchronized void method2() { } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
public class Test { public void method1() { synchronized ( this ) { } } public synchronized void method2() { } } |
在內類中使用synchronized塊中,this只表示內類,和外類(OuterClass)沒有關系。但是內類中的非靜態方法和外類的非靜態方法也可以同步。如果在內類中加個方法method3也可以使和Test里面的2個方法同步,代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Test { class InnerClass { public void method3() { synchronized (Test. this ){ } } } } |
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Test { class InnerClass { public void method3() { synchronized (Test. this ){ } } } } |
上面InnerClass的method3方法與Test的method1和method2方法在同一時間內只能有一個方法執行。
synchronized塊不管是正確執行完,還是因為程序出錯因異常退出synchronized塊,當前的synchronized塊所持有的同步鎖都會自動釋放,因此在使用synchronized塊不必擔心同步鎖的問題。
二、靜態方法的同步
由于在調用靜態方法時,對象實例不一定被創建,因此,就不能使用this來同步靜態方法,而必須使用Class對象來同步靜態方法。代碼如下:
1
2
3
4
5
6
7
8
9
10
|
public class Test{ pubic static void method1(){ synchronized (Test. class ){ } } public static synchronized void method2(){ } } |
1
2
3
4
5
6
7
8
9
10
|
public class Test{ pubic static void method1(){ synchronized (Test. class ){ } } public static synchronized void method2(){ } } |
在同步靜態方法時可以使用類的靜態字段class來得到class對象,在上例中method1和method2方法只有一個方法執行,除了使用class字段可以得到class對象,還可以通過實例的getClass()方法獲取class對象,代碼如下:
1
2
3
4
5
6
7
8
9
10
|
public class Test{ public static Test test; public Test(){ test= this ; } public static void method1(){ synchronized (test.getClass()){ } } } |
1
2
3
4
5
6
7
8
9
10
|
public class Test{ public static Test test; public Test(){ test= this ; } public static void method1(){ synchronized (test.getClass()){ } } } |
在上面的代碼中,我們通過一個public的靜態對象得到Test的一個實例,并通過這個實例的getClass方法獲取一個class對象(注意一個類的所有實例通過getClass方法得到的都是同一個Class對象)。我們也可以通過class使不同類的靜態方法同步,代碼如下:
Test類中的方法和Test1類中方法同步。
1
2
3
4
5
6
|
public class Test1{ public static void method1(){ synchronized (Test. class ){ } } } |
1
2
3
4
5
6
|
public class Test1{ public static void method1(){ synchronized (Test. class ){ } } } |
注意:在使用synchronized塊來同步方法時,非靜態方法可以通過this來同步,而靜態方法必須使用class對象來同步,但是非靜態方法也可以通過使用class來同步靜態方法。但是靜態方法中不能使用this來同步非靜態方法。這點在使用synchronized塊需要注意。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
原文鏈接:http://blog.csdn.net/jenny8080/article/details/19623057