Java 類鎖、對象鎖、私有鎖


3.6 Java類鎖、對象鎖、私有鎖、隱式鎖


類鎖對象鎖是否會沖突?對象鎖和私有鎖是否會沖突?通過實例來進行說明。

一、相關約定

為了明確后文的描述,先對本文涉及到的鎖的相關定義作如下約定:

1. 類鎖:在代碼中的方法上加了static和synchronized的鎖,或者synchronized(xxx.class)的代碼段,如下文中的increament();

2.對象鎖:在代碼中的方法上加了synchronized的鎖,或者synchronized(this)的代碼段,如下文中的synOnMethod()和synInMethod();

3.私有鎖:在類內部聲明一個私有屬性如private Object lock,在需要加鎖的代碼段synchronized(lock),如下文中的synMethodWithObj()。

二、測試代碼

1.編寫一個啟動類ObjectLock


    
    
   
   
           
  1. public class ObjectLock {
  2.     public static void main(String[] args) {
  3.         System.out.println("start time = " + System.currentTimeMillis()+"ms");
  4.         LockTestClass test = new LockTestClass();
  5.         for (int i = 0; i < 3; i++) {
  6.             Thread thread = new ObjThread(test, i);
  7.             thread.start();
  8.         }
  9.     }
  10. }

    2.編寫一個線程類ObjThread,用於啟動同步方法(注意它的run方法可能會調整以進行不同的測試)

    
        
        
       
       
               
    1. public class ObjThread extends Thread {
    2.     LockTestClass lock;
    3.     int i = 0;
    4.  
    5.     public ObjThread(LockTestClass lock, int i) {
    6.         this.lock = lock;
    7.         this.= i;
    8.     }
    9.  
    10.     public void run() {
    11.         //無鎖方法
    12. //      lock.noSynMethod(this.getId(),this);
    13.         //對象鎖方法1,采用synchronized synInMethod的方式
    14.         lock.synInMethod();
    15.         //對象鎖方法2,采用synchronized(this)的方式
    16. //      lock.synOnMethod();
    17.         //私有鎖方法,采用synchronized(object)的方式
    18. //      lock.synMethodWithObj();
    19.         //類鎖方法,采用static synchronized increment的方式
    20.         LockTestClass.increment();
    21.     }
    22. }

      3.再編寫一個鎖的測試類LockTestClass,包括各種加鎖方法

      
          
          
         
         
                 
      1. public class LockTestClass {
      2.     //用於類鎖計數
      3.     private static int i = 0;
      4.     //私有鎖
      5.     private Object object = new Object();
      6.  
      7.     /**
      8.      * &lt;p&gt;
      9.      * 無鎖方法
      10.      *
      11.      * @param threadID
      12.      * @param thread
      13.      */
      14.     public void noSynMethod(long threadID, ObjThread thread) {
      15.         System.out.println("nosyn: class obj is " + thread + ", threadId is"
      16.                 + threadID);
      17.     }
      18.  
      19.     /**
      20.      * 對象鎖方法1
      21.      */
      22.     public synchronized void synOnMethod() {
      23.         System.out.println("synOnMethod begins" + ", time = "
      24.                 + System.currentTimeMillis() + "ms");
      25.         try {
      26.             Thread.sleep(2000L);
      27.         } catch (InterruptedException e) {
      28.             e.printStackTrace();
      29.         }
      30.         System.out.println("synOnMethod ends");
      31.     }
      32.  
      33.     /**
      34.      * 對象鎖方法2,采用synchronized (this)來加鎖
      35.      */
      36.     public void synInMethod() {
      37.         synchronized (this) {
      38.             System.out.println("synInMethod begins" + ", time = "
      39.                     + System.currentTimeMillis() + "ms");
      40.             try {
      41.                 Thread.sleep(2000L);
      42.             } catch (InterruptedException e) {
      43.                 e.printStackTrace();
      44.             }
      45.             System.out.println("synInMethod ends");
      46.         }
      47.  
      48.     }
      49.  
      50.     /**
      51.      * 對象鎖方法3
      52.      */
      53.     public void synMethodWithObj() {
      54.         synchronized (object) {
      55.             System.out.println("synMethodWithObj begins" + ", time = "
      56.                     + System.currentTimeMillis() + "ms");
      57.             try {
      58.                 Thread.sleep(2000L);
      59.             } catch (InterruptedException e) {
      60.                 e.printStackTrace();
      61.             }
      62.             System.out.println("synMethodWithObj ends");
      63.         }
      64.     }
      65.  
      66.     /**
      67.      * 類鎖
      68.      */
      69.     public static synchronized void increament() {
      70.         System.out.println("class synchronized. i = " + i + ", time = "
      71.                 + System.currentTimeMillis() + "ms");
      72.         i++;
      73.         try {
      74.             Thread.sleep(2000L);
      75.         } catch (InterruptedException e) {
      76.             e.printStackTrace();
      77.         }
      78.          System.out.println("class synchronized ends.");
      79.     }
      80.  
      81. }

        三、測試結果

        1. 測試類鎖和對象鎖,ObjectThread的run方法修改如下:

        
            
            
           
           
                   
        1. public void run() {
        2.         //無鎖方法
        3. //      lock.noSynMethod(this.getId(),this);
        4.         //對象鎖方法1,采用synchronized synInMethod的方式
        5.         lock.synInMethod();
        6.         //對象鎖方法2,采用synchronized(this)的方式
        7. //      lock.synOnMethod();
        8.         //私有鎖方法,采用synchronized(object)的方式
        9. //      lock.synMethodWithObj();
        10.         //類鎖方法,采用static synchronized increment的方式
        11.         LockTestClass.increament();
        12.     }

          終端輸出:

          
              
              
             
             
                     
          1. start time = 1413101360231ms
          2.  synInMethod begins, time = 1413101360233ms
          3.  synInMethod ends
          4.  class synchronized. i = 0, time = 1413101362233ms
          5.  synInMethod begins, time = 1413101362233ms 
          6.  class synchronized ends. 
          7.  synInMethod ends 
          8.  class synchronized. i = 1, time = 1413101364233ms 
          9.  synInMethod begins, time = 1413101364233ms 
          10.  class synchronized ends. 
          11.  synInMethod ends 
          12.  class synchronized. i = 2, time = 1413101366234ms 
          13.  class synchronized ends.

            可以看到對象鎖方法(synInMothod)第一次啟動時比類鎖方法(increament)快2秒,這是因為在synInMehtod執行時 sleep了2秒再執行的increament,而這兩個方法共用一個線程,所以會慢2秒,如果increament在run中放到 synInMethod前面,那么第一次啟動時就是increament快2秒。

            而當類鎖方法啟動時,另一個線程時的對象鎖方法也幾乎同時啟動,說明二者使用的並非同一個鎖,不會產生競爭。

            結論:類鎖和對象鎖不會產生競爭,二者的加鎖方法不會相互影響。

            2.私有鎖和對象鎖,ObjectThread的run方法修改如下:

            
                
                
               
               
                       
            1. public void run() {
            2.         //無鎖方法
            3. //      lock.noSynMethod(this.getId(),this);
            4.         //對象鎖方法1,采用synchronized synInMethod的方式
            5.         lock.synInMethod();
            6.         //對象鎖方法2,采用synchronized(this)的方式
            7. //      lock.synOnMethod();
            8.         //私有鎖方法,采用synchronized(object)的方式
            9.         lock.synMethodWithObj();
            10.         //類鎖方法,采用static synchronized increment的方式
            11. //      LockTestClass.increament();
            12.     }

              終端輸出:

              
                  
                  
                 
                 
                         
              1. start time = 1413121912406ms 
              2. synInMethod begins, time = 1413121912407ms. 
              3. synInMethod ends. 
              4. synMethodWithObj begins, time = 1413121914407ms 
              5. synInMethod begins, time = 1413121914407ms. 
              6. synInMethod ends. 
              7. synMethodWithObj ends 
              8. synInMethod begins, time = 1413121916407ms. 
              9. synMethodWithObj begins, time = 1413121916407ms 
              10. synInMethod ends. 
              11. synMethodWithObj ends 
              12. synMethodWithObj begins, time = 1413121918407ms 
              13. synMethodWithObj ends

                和類鎖和對象鎖非常類似。

                結論:私有鎖和對象鎖也不會產生競爭,二者的加鎖方法不會相互影響。

                3.synchronized直接加在方法上和synchronized(this),ObjectThread的run方法修改如下:

                
                    
                    
                   
                   
                           
                1. public void run() { 
                2. //無鎖方法 // lock.noSynMethod(this.getId(),this); 
                3. //對象鎖方法1,采用synchronized synInMethod的方式 
                4. lock.synInMethod(); 
                5. //對象鎖方法2,采用synchronized(this)的方式 
                6. lock.synOnMethod(); 
                7. //私有鎖方法,采用synchronized(object)的方式 
                8. // lock.synMethodWithObj(); 
                9. //類鎖方法,采用static synchronized increment的方式 
                10. // LockTestClass.increament(); 
                11. }

                  終端輸出:

                  
                      
                      
                     
                     
                             
                  1. start time = 1413102913278ms 
                  2. synInMethod begins, time = 1413102913279ms 
                  3. synInMethod ends 
                  4. synInMethod begins, time = 1413102915279ms 
                  5. synInMethod ends 
                  6. synOnMethod begins, time = 1413102917279ms 
                  7. synOnMethod ends 
                  8. synInMethod begins, time = 1413102919279ms 
                  9. synInMethod ends 
                  10. synOnMethod begins, time = 1413102921279ms 
                  11. synOnMethod ends 
                  12. synOnMethod begins, time = 1413102923279ms 
                  13. synOnMethod ends

                     可以看到,二者嚴格地串行輸出(當然再次執行時先運行synInMethod還是先運行synOnMethod並不是確定的,取決於誰獲得了鎖)。 

                    結論:synchronized直接加在方法上和synchronized(this)都是對當前對象加鎖,二者的加鎖方法夠成了競爭關系,同一時刻只能有一個方法能執行。   



                    免責聲明!

                    本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



                     
                    粵ICP備18138465號   © 2018-2025 CODEPRJ.COM