1. 程式人生 > >synchronized 用在例項方法和類方法的區別

synchronized 用在例項方法和類方法的區別

在Java中,synchronized 是用來表示同步的,我們可以synchronized 來修飾一個方法。也可以synchronized 來修飾方法裡面的一個語句塊。

      修飾例項方法:

Java程式碼  收藏程式碼
  1. publicsynchronizedvoid normalMethod() throws InterruptedException {  
  2.     for (int i = 0; i < 10; i++) {  
  3.         Thread.sleep(1000);  
  4.         System.out.println("normalMethod:" + i);  
  5.     }  
  6. }  

      修飾類方法(static 方法):

Java程式碼  收藏程式碼
  1. publicstaticsynchronizedvoid staticMethod() 
    throws InterruptedException {  
  2.     for (int i = 0; i < 10; i++) {  
  3.         Thread.sleep(500);  
  4.         System.out.println("staticMethod:" + i);  
  5.     }  
  6. }  

       修飾方法裡面語句塊:

Java程式碼  收藏程式碼
  1. publicstaticvoid staticMethod() throws InterruptedException  {    
  2.         synchronized (locks) {    
  3.             for
     (int i = 0; i < 10; i++)  {    
  4.                 Thread.sleep(1000);    
  5.                 System.out.println("staticMethod:" + i);    
  6.            }    
  7.        }    
  8. }    

      注意:這裡不能用synchronized修飾方法外面的語句塊(我把他叫做類語句塊),雖然我們可以在方法外面定義語句塊,這樣做會遇到編譯錯誤,這裡涉及到了Java裡面的物件初始化的部分知識。大概的原因就是synchronized鎖住的是物件,當初始化物件的時候,JVM在物件初始化完成之前會呼叫方法外面的語句塊,這個時候物件還不存在,所以就不存在鎖了。

      那麼,在static方法和非static方法前面加synchronized到底有什麼不同呢?

      static的方法屬於類方法,它屬於這個Class(注意:這裡的Class不是指Class的某個具體物件),那麼static獲取到的鎖,就是當前呼叫這個方法的物件所屬的類(Class,而不再是由這個Class產生的某個具體物件了)。而非static方法獲取到的鎖,就是當前呼叫這個方法的物件的鎖了。所以,他們之間不會產生互斥。

      例項1:

Java程式碼  收藏程式碼
  1. package com.bijian.thread;  
  2. publicclass SynchronizedTest {  
  3.     publicstaticsynchronizedvoid staticMethod() throws InterruptedException {  
  4.         for (int i = 0; i < 10; i++) {  
  5.             Thread.sleep(500);  
  6.             System.out.println("staticMethod:" + i);  
  7.         }  
  8.     }  
  9.     publicsynchronizedvoid normalMethod() throws InterruptedException {  
  10.         for (int i = 0; i < 10; i++) {  
  11.             Thread.sleep(1000);  
  12.             System.out.println("normalMethod:" + i);  
  13.         }  
  14.     }  
  15.     publicstaticvoid main(String[] args) {  
  16.         final SynchronizedTest synchronizedTest = new SynchronizedTest();  
  17.         Thread thread = new Thread(new Runnable() {  
  18.             publicvoid run() {  
  19.                 try {  
  20.                     synchronizedTest.normalMethod();  
  21.                 } catch (InterruptedException e) {  
  22.                     e.printStackTrace();  
  23.                 }  
  24.             }  
  25.         }, "a");  
  26.         Thread thread1 = new Thread(new Runnable() {  
  27.             publicvoid run() {  
  28.                 try {  
  29.                     SynchronizedTest.staticMethod();  
  30.                 } catch (InterruptedException e) {  
  31.                     e.printStackTrace();  
  32.                 }  
  33.             }  
  34.         }, "b");  
  35.         thread1.start();  
  36.         thread.start();  
  37.     }  
  38. }  

       執行結果:

Text程式碼  收藏程式碼
  1. staticMethod:0
  2. normalMethod:0
  3. staticMethod:1
  4. staticMethod:2
  5. normalMethod:1
  6. staticMethod:3
  7. staticMethod:4
  8. normalMethod:2
  9. staticMethod:5
  10. staticMethod:6
  11. normalMethod:3
  12. staticMethod:7
  13. staticMethod:8
  14. normalMethod:4
  15. staticMethod:9
  16. normalMethod:5
  17. normalMethod:6
  18. normalMethod:7
  19. normalMethod:8
  20. normalMethod:9

       那當我們想讓所有這個類下面的物件都同步的時候,也就是讓所有這個類下面的物件共用同一把鎖的時候,我們如何辦呢?

       法1:將normalMethod方法也改成static,這樣這兩個static方法都屬於類方法,它們獲取到的鎖都是當前呼叫這個方法的物件所屬的類(Class,而不再是由這個Class產生的某個具體物件了)。但這樣會影響程式碼結構和物件的封裝性。

       修改例項1如下:

Java程式碼  收藏程式碼
  1. package com.bijian.thread;  
  2. publicclass SynchronizedTest {  
  3.     publicstaticsynchronizedvoid staticMethod() throws InterruptedException {  
  4.         for (int i = 0; i < 10; i++) {  
  5.             Thread.sleep(500);  
  6.             System.out.println("staticMethod:" + i);  
  7.         }  
  8.     }  
  9.     publicstaticsynchronizedvoid normalMethod() throws InterruptedException {  
  10.         for (int i = 0; i < 10; i++) {  
  11.             Thread.sleep(1000);  
  12.             System.out.println("normalMethod:" + i);  
  13.         }  
  14.     }  
  15.     publicstaticvoid main(String[] args) {  
  16.         Thread thread = new Thread(new Runnable() {  
  17.             publicvoid run() {  
  18.                 try {  
  19.                     SynchronizedTest.normalMethod();  
  20.                 } catch (InterruptedException e) {  
  21.                     e.printStackTrace();  
  22.                 }  
  23.             }  
  24.         }, "a");  
  25.         Thread thread1 = new Thread(new Runnable() {  
  26.             publicvoid run() {  
  27.                 try {  
  28.                     SynchronizedTest.staticMethod();  
  29.                 } catch (InterruptedException e) {  
  30.                     e.printStackTrace();  
  31.                 }  
  32.             }  
  33.         }, "b");  
  34.         thread1.start();  
  35.         thread.start();  
  36.     }  
  37. }  

       執行結果:

Text程式碼  收藏程式碼
  1. staticMethod:0
  2. staticMethod:1
  3. staticMethod:2
  4. staticMethod:3
  5. staticMethod:4
  6. staticMethod:5
  7. staticMethod:6
  8. staticMethod:7
  9. staticMethod:8
  10. staticMethod:9
  11. normalMethod:0
  12. normalMethod:1
  13. normalMethod:2
  14. normalMethod:3
  15. normalMethod:4
  16. normalMethod:5
  17. normalMethod:6
  18. normalMethod:7
  19. normalMethod:8
  20. normalMethod:9

       也許有人說:將例項1的staticMethod方法改成的static去掉也能達到目的。確實可以,因為非static方法獲取到的鎖,就是當前呼叫這個方法的物件的鎖,而例項1只有一個SynchronizedTest例項,如再建立一個例項,則就有問題了。如下所示:

Java程式碼  收藏程式碼
  1. package com.bijian.thread;  
  2. publicclass SynchronizedTest {  
  3.     publicsynchronizedvoid staticMethod() throws InterruptedException {  
  4.         for (int i = 0; i < 10; i++) {  
  5.             Thread.sleep(500);  
  6.             System.out.println("staticMethod:" + i);  
  7.         }  
  8.     }  
  9.     publicsynchronizedvoid normalMethod() throws InterruptedException {  
  10.         for (int i = 0; i < 10; i++) {  
  11.             Thread.sleep(1000);  
  12.             System.out.println("normalMethod:" + i);  
  13.         }  
  14.     }  
  15.     publicstaticvoid main(String[] args) {  
  16.         final SynchronizedTest synchronizedTest = new SynchronizedTest();  
  17.         Thread thread = new Thread(new Runnable() {  
  18.             publicvoid run() {  
  19.                 try {  
  20.                     synchronizedTest.normalMethod();  
  21.                 } catch (InterruptedException e) {  
  22.                     e.printStackTrace();  
  23.                 }  
  24.             }  
  25.         }, "a");  
  26.         //為了驗證獲取到的鎖都是當前呼叫這個方法的物件所屬的類,特另新建一個物件
  27.         final SynchronizedTest synchronizedTest2 = new SynchronizedTest();  
  28.         Thread thread1 = new Thread(new Runnable() {  
  29.             publicvoid run() {  
  30.                 try {  
  31.                     synchronizedTest2.staticMethod();  
  32.                 } catch (InterruptedException e) {  
  33.                     e.printStackTrace();  
  34.                 }  
  35.             }  
  36.         }, "b");  
  37.         thread1.start();  
  38.         thread.start();  
  39.     }  
  40. }  

       執行結果:

Text程式碼  收藏程式碼
  1. staticMethod:0
  2. staticMethod:1
  3. normalMethod:0
  4. staticMethod:2
  5. staticMethod:3
  6. normalMethod:1
  7. staticMethod:4
  8. staticMethod:5
  9. normalMethod:2
  10. staticMethod:6
  11. normalMethod:3
  12. staticMethod:7
  13. staticMethod:8
  14. normalMethod:4
  15. staticMethod:9
  16. normalMethod:5
  17. normalMethod:6
  18. normalMethod:7
  19. normalMethod:8
  20. normalMethod:9

       法2:語句塊鎖,直接看如下例項:

       例項2:

Java程式碼  收藏程式碼
  1. package com.bijian.thread;  
  2. publicclass SynchronizedTest {  
  3.     publicfinalstatic Byte[] locks = new Byte[0];    
  4.     publicstaticvoid staticMethod() throws InterruptedException {  
  5.         synchronized(locks) {  
  6.             for (int i = 0; i < 10; i++) {  
  7.                 Thread.sleep(500);  
  8.                 System.out.println("staticMethod:" + i);  
  9.             }  
  10.         }  
  11.     }  
  12.     publicvoid normalMethod() throws InterruptedException {  
  13.         synchronized(locks) {  
  14.             for (int i = 0; i < 10; i++) {  
  15.                 Thread.sleep(1000);  
  16.                 System.out.println("normalMethod:" + i);  
  17.             }  
  18.         }  
  19.     }  
  20.     publicstaticvoid main(String[] args) {  
  21.         final SynchronizedTest synchronizedTest = new SynchronizedTest();  
  22.         Thread thread = new Thread(new Runnable() {  
  23.             publicvoid run() {  
  24.                 try {  
  25.                     synchronizedTest.normalMethod();  
  26.                 } catch (InterruptedException e) {  
  27.                     e.printStackTrace();  
  28.                 }  
  29.             }  
  30.         }, "a");  
  31.         Thread thread1 = new Thread(new Runnable() {  
  32.             publicvoid run() {  
  33.                 try {  
  34.                     SynchronizedTest.staticMethod();  
  35.                 } catch (InterruptedException e) {  
  36.                     e.printStackTrace();  
  37.                 }  
  38.             }  
  39.         }, "b");  
  40.         thread1.start();  
  41.         thread.start();  
  42.     }  
  43. }  

       執行結果:

Text程式碼  收藏程式碼
  1. staticMethod:0
  2. staticMethod:1
  3. staticMethod:2
  4. staticMethod:3
  5. staticMethod:4
  6. staticMethod:5
  7. staticMethod:6
  8. staticMethod:7
  9. staticMethod:8
  10. staticMethod:9
  11. normalMethod:0
  12. normalMethod:1
  13. normalMethod:2
  14. normalMethod:3
  15. normalMethod:4
  16. normalMethod:5
  17. normalMethod:6
  18. normalMethod:7
  19. normalMethod:8
  20. normalMethod:9