synchronized的4种用法

  1.方法声明时使用,放在范围操作符(public等)之后,返回类型声明(void等)之前.这时,线程获得的是成员锁,即一次只能有一个线程进入该方法,其他线程要想在此时调用该方法,只能排队等候,当前线程(就是在synchronized方法内部的线程)执行完该方法后,别的线程才能进入.
 
      例如:

ExpandedBlockStart.gif      public synchronized void synMethod() {
InBlock.gif        //方法体
ExpandedBlockEnd.gif
      }

    2.对某一代码块使用,synchronized后跟括号,括号里是变量,这样,一次只有一个线程进入该代码块.此时,线程获得的是成员锁.例如:

ExpandedBlockStart.gif      public int synMethod(int a1){
ExpandedSubBlockStart.gif        synchronized(a1) {
InBlock.gif          //一次只能有一个线程进入
ExpandedSubBlockEnd.gif
        }

ExpandedBlockEnd.gif      }


    3.synchronized后面括号里是一对象,此时,线程获得的是对象锁.例如:

 

ExpandedBlockStart.gif public class MyThread implements Runnable {
ExpandedSubBlockStart.gif    public static void main(String args[]) {
InBlock.gif    MyThread mt = new MyThread();
InBlock.gif    Thread t1 = new Thread(mt, "t1");
InBlock.gif    Thread t2 = new Thread(mt, "t2");
InBlock.gif    Thread t3 = new Thread(mt, "t3");
InBlock.gif    Thread t4 = new Thread(mt, "t4");
InBlock.gif    Thread t5 = new Thread(mt, "t5");
InBlock.gif    Thread t6 = new Thread(mt, "t6");
InBlock.gif    t1.start();
InBlock.gif    t2.start();
InBlock.gif    t3.start();
InBlock.gif    t4.start();
InBlock.gif    t5.start();
InBlock.gif    t6.start();
ExpandedSubBlockEnd.gif  }

InBlock.gif
ExpandedSubBlockStart.gif  public void run() {
ExpandedSubBlockStart.gif    synchronized (this{
InBlock.gif      System.out.println(Thread.currentThread().getName());
ExpandedSubBlockEnd.gif    }

ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif}
 
None.gif 
None.gif
None.gif

 
    对于3,如果线程进入,则得到当前对象锁,那么别的线程在该类所有对象上的任何操作都不能进行.在对象级使用锁通常是一种比较粗糙的方法。为什么要将整个对象都上锁,而不允许其他线程短暂地使用对象中其他同步方法来访问共享资源?如果一个对象拥有多个资源,就不需要只为了让一个线程使用其中一部分资源,就将所有线程都锁在外面。由于每个对象都有锁,可以如下所示使用虚拟对象来上锁:

 

ExpandedBlockStart.gif class FineGrainLock {
InBlock.gif
InBlock.gif   MyMemberClass x, y;
InBlock.gif   Object xlock = new Object(), ylock = new Object();
InBlock.gif
ExpandedSubBlockStart.gif   public void foo() {
ExpandedSubBlockStart.gif      synchronized(xlock) {
InBlock.gif         //access x here
ExpandedSubBlockEnd.gif
      }

InBlock.gif
InBlock.gif      //do something here - but don't use shared resources
InBlock.gif

ExpandedSubBlockStart.gif      synchronized(ylock) {
InBlock.gif         //access y here
ExpandedSubBlockEnd.gif
      }

ExpandedSubBlockEnd.gif   }

InBlock.gif
ExpandedSubBlockStart.gif   public void bar() {
ExpandedSubBlockStart.gif      synchronized(this{
InBlock.gif         //access both x and y here
ExpandedSubBlockEnd.gif
      }

InBlock.gif      //do something here - but don't use shared resources
ExpandedSubBlockEnd.gif
   }

ExpandedBlockEnd.gif  }

None.gif
None.gif


     4.synchronized后面括号里是类,此时,线程获得的是对象锁.例如:

 

ExpandedBlockStart.gifclass ArrayWithLockOrder{
InBlock.gif  private static long num_locks = 0;
InBlock.gif  private long lock_order;
InBlock.gif  private int[] arr;
InBlock.gif
InBlock.gif  public ArrayWithLockOrder(int[] a)
ExpandedSubBlockStart.gif  {
InBlock.gif    arr = a;
ExpandedSubBlockStart.gif    synchronized(ArrayWithLockOrder.class{//-----这里
InBlock.gif
      num_locks++;             // 锁数加 1。
InBlock.gif

InBlock.gif      lock_order = num_locks;  // 为此对象实例设置唯一的 lock_order。
ExpandedSubBlockEnd.gif
    }

ExpandedSubBlockEnd.gif  }

InBlock.gif  public long lockOrder()
ExpandedSubBlockStart.gif  {
InBlock.gif    return lock_order;
ExpandedSubBlockEnd.gif  }

InBlock.gif  public int[] array()
ExpandedSubBlockStart.gif  {
InBlock.gif    return arr;
ExpandedSubBlockEnd.gif  }

ExpandedBlockEnd.gif  }

None.gif
None.gif  
class SomeClass implements Runnable
ExpandedBlockStart.gif 
{
InBlock.gif  public int sumArrays(ArrayWithLockOrder a1,
InBlock.gif                       ArrayWithLockOrder a2)
ExpandedSubBlockStart.gif  {
InBlock.gif    int value = 0;
InBlock.gif    ArrayWithLockOrder first = a1;       // 保留数组引用的一个
InBlock.gif
    ArrayWithLockOrder last = a2;        // 本地副本。
InBlock.gif
    int size = a1.array().length;
InBlock.gif    if (size == a2.array().length)
ExpandedSubBlockStart.gif    {
InBlock.gif      if (a1.lockOrder() > a2.lockOrder())  // 确定并设置对象的锁定
ExpandedSubBlockStart.gif
      {                                     // 顺序。
InBlock.gif
        first = a2;
InBlock.gif        last = a1;
ExpandedSubBlockEnd.gif      }

ExpandedSubBlockStart.gif      synchronized(first) {              // 按正确的顺序锁定对象。
ExpandedSubBlockStart.gif
        synchronized(last) {
InBlock.gif          int[] arr1 = a1.array();
InBlock.gif          int[] arr2 = a2.array();
InBlock.gif          for (int i=0; i<size; i++)
InBlock.gif            value += arr1[i] + arr2[i];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif      }

ExpandedSubBlockEnd.gif    }

InBlock.gif    return value;
InBlock.gif
ExpandedSubBlockEnd.gif  }

ExpandedSubBlockStart.gif  public void run() {
InBlock.gif    //dot.gif
ExpandedSubBlockEnd.gif
  }

ExpandedBlockEnd.gif  }

None.gif
None.gif

  
    对于4,如果线程进入,则线程在该类中所有操作不能进行,包括静态变量和静态方法,实际上,对于含有静态方法和静态变量的代码块的同步,我们通常用4来加锁.


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