synchronized的4種用法

轉載自:http://hi.baidu.com/wojiubaibudu/blog/item/a27d671ed654cae6e1fe0b42.html

1.方法聲明時使用,放在範圍操作符(public等)之後,返回類型聲明(void等)之前.即一次只能有一個線程進入該方法,其他線程要想在此時調用該方法,只能排隊等候,當前線程(就是在synchronized方法內部的線程)執行完該方法後,別的線程才能進入.

  1. 例如: 
  2.      public synchronized void synMethod() { 
  3.        //方法體 
  4.      } 

2.對某一代碼塊使用,synchronized後跟括號,括號裏是變量,這樣,一次只有一個線程進入該代碼塊.例如:

  1. public int synMethod(int a1){ 
  2.         synchronized(a1) { 
  3.           //一次只能有一個線程進入 
  4.         } 
  5.       } 

3.synchronized後面括號裏是一對象,此時,線程獲得的是對象鎖.例如:

  1. public class MyThread implements Runnable { 
  2.   public static void main(String args[]) { 
  3.     MyThread mt = new MyThread(); 
  4.     Thread t1 = new Thread(mt, "t1"); 
  5.     Thread t2 = new Thread(mt, "t2"); 
  6.     Thread t3 = new Thread(mt, "t3"); 
  7.     Thread t4 = new Thread(mt, "t4"); 
  8.     Thread t5 = new Thread(mt, "t5"); 
  9.     Thread t6 = new Thread(mt, "t6"); 
  10.     t1.start(); 
  11.     t2.start(); 
  12.     t3.start(); 
  13.     t4.start(); 
  14.     t5.start(); 
  15.     t6.start(); 
  16.   } 
  17.  
  18.   public void run() { 
  19.     synchronized (this) { 
  20.       System.out.println(Thread.currentThread().getName()); 
  21.     } 
  22.   } 

    對於3,如果線程進入,則得到對象鎖,那麼別的線程在該類所有對象上的任何操作都不能進行.在對象級使用鎖通常是一種比較粗糙的方法。爲什麼要將整個對象 都上鎖,而不允許其他線程短暫地使用對象中其他同步方法來訪問共享資源?如果一個對象擁有多個資源,就不需要只爲了讓一個線程使用其中一部分資源,就將所 有線程都鎖在外面。由於每個對象都有鎖,可以如下所示使用虛擬對象來上鎖:

 

  1. class FineGrainLock { 
  2.  
  3.    MyMemberClass x, y; 
  4.    Object xlock = new Object(), ylock = new Object(); 
  5.  
  6.    public void foo() { 
  7.       synchronized(xlock) { 
  8.          //access x here 
  9.       } 
  10.  
  11.       //do something here - but don‘t use shared resources 
  12.  
  13.       synchronized(ylock) { 
  14.          //access y here 
  15.       } 
  16.    } 
  17.  
  18.    public void bar() { 
  19.       synchronized(this) { 
  20.          //access both x and y here 
  21.       } 
  22.       //do something here - but don‘t use shared resources 
  23.    } 

4.synchronized後面括號裏是類.例如:

  1. class ArrayWithLockOrder{ 
  2.   private static long num_locks = 0
  3.   private long lock_order; 
  4.   private int[] arr; 
  5.  
  6.   public ArrayWithLockOrder(int[] a) 
  7.   { 
  8.     aarr = a; 
  9.     synchronized(ArrayWithLockOrder.class) {//-----------------------------------------這裏 
  10.       num_locks++;             // 鎖數加 1。 
  11.       lock_order = num_locks;  // 爲此對象實例設置唯一的 lock_order。 
  12.     } 
  13.   } 
  14.   public long lockOrder() 
  15.   { 
  16.     return lock_order; 
  17.   } 
  18.   public int[] array() 
  19.   { 
  20.     return arr; 
  21.   } 
  22.  
  23. class SomeClass implements Runnable 
  24.   public int sumArrays(ArrayWithLockOrder a1, 
  25.                        ArrayWithLockOrder a2) 
  26.   { 
  27.     int value = 0
  28.     ArrayWithLockOrder first = a1;       // 保留數組引用的一個 
  29.     ArrayWithLockOrder last = a2;        // 本地副本。 
  30.     int size = a1.array().length; 
  31.     if (size == a2.array().length) 
  32.     { 
  33.       if (a1.lockOrder() > a2.lockOrder())  // 確定並設置對象的鎖定 
  34.       {                                     // 順序。 
  35.         first = a2
  36.         last = a1
  37.       } 
  38.       synchronized(first) {              // 按正確的順序鎖定對象。 
  39.         synchronized(last) { 
  40.           int[] arr1 = a1.array(); 
  41.           int[] arr2 = a2.array(); 
  42.           for (int i=0; i            value += arr1[i] + arr2[i]; 
  43.         } 
  44.       } 
  45.     } 
  46.     return value; 
  47.   } 
  48.   public void run() { 
  49.     //... 
  50.   } 

    對於4,如果線程進入,則線程在該類中所有操作不能進行,包括靜態變量和靜態方法,實際上,對於含有靜態方法和靜態變量的代碼塊的同步,我們通常用4來加鎖.

以上4種之間的關係:

    鎖 是和對象相關聯的,每個對象有一把鎖,爲了執行synchronized語句,線程必須能夠獲得synchronized語句中表達式指定的對象的鎖,一 個對象只有一把鎖,被一個線程獲得之後它就不再擁有這把鎖,線程在執行完synchronized語句後,將獲得鎖交還給對象。
    在方法前面加上synchronized修飾符即可以將一個方法聲明爲同步化方法。同步化方法在執行之前獲得一個鎖。如果這是一個類方法,那麼獲得的鎖是和聲明方法的類相關的Class類對象的鎖。如果這是一個實例方法,那麼此鎖是this對象的鎖。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章