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对象的锁。

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