(47)21.4.3 中断2---Java编程思想之并发笔记

来源:互联网 发布:2017年大学招生数据 编辑:程序博客网 时间:2024/04/30 01:08
被互斥所阻塞
1. 在一个对象上调用synchronized方法,而这个对象在锁已经被其它对象获得,那么调用任务就被挂起,直到这个锁可以获得。
2. 例子:
package jiangning.c21;

public class MultiLock {
       public synchronized void f1(int count){
             if(count-- > 0){
                  System. out.println("f1() call f2() with count" +count);
                  f2(count);
            }
      }
       public synchronized void f2(int count){
             if(count-- > 0){
                  System. out.println("f2() call f1() with count" +count);
                  f1(count);
            }
      }
       public static void main(String[] args) {
             final MultiLock multiLock = new MultiLock();
             new Thread(){
                   public void run(){
                        multiLock.f1(10);
                  }
            }.start();
      }

}


/**
f2() call f1() with count8
f1() call f2() with count7
f2() call f1() with count6
f1() call f2() with count5
f2() call f1() with count4
f1() call f2() with count3
f2() call f1() with count2
f1() call f2() with count1
f2() call f1() with count0
*/

3.ReenTRantLock
  这个类是在Java SE5增加的新特性。
4.例子2.
package jiangning.c21;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class BlockedMutex{
       private Lock lock = new ReentrantLock();
       public BlockedMutex(){
             lock.lock();//在初始化的时候锁定了对象,没有进行释放
      }
       public void f(){
             try {
                   lock.lockInterruptibly();
                  System. out.println("lock acquired in f()" );
            } catch (InterruptedException e) {
                  e.printStackTrace();
                  System. out.println(" Interrupted from lock acquisition in f()");
            }
      }
}
class Blocked2 implements Runnable{
      BlockedMutex blocked = new BlockedMutex();
      
       public void run() {
            System. out.println("Waiting for f() in BlockedMutex" );      
             blocked.f();//被阻塞,在这里想调用f方法,但是没有释放对象,从而抛出异常。
            System. out.println("Broken out of blocked call" );
      }
      
}
public class Interrupting2 {

       public static void main(String[] args) throws Exception{
            Thread t = new Thread(new Blocked2());
            t.start();
            TimeUnit. SECONDS.sleep(1);
            System. out.println("Issuing t.interrupt()" );
            t.interrupt(); //打断被互斥所阻塞的调用。
      }
}

/**
Waiting for f() in BlockedMutex
Issuing t.interrupt()
java.lang.InterruptedException Interrupted from lock acquisition in f()
Broken out of blocked call

       at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(AbstractQueuedSynchronizer.java:877)
       at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(AbstractQueuedSynchronizer.java:1201)
       at java.util.concurrent.locks.ReentrantLock.lockInterruptibly(ReentrantLock.java:312)
       at jiangning.c21.BlockedMutex.f(Interrupting2.java:14)
       at jiangning.c21.Blocked2.run(Interrupting2.java:27)
       at java.lang .Thread.run(Thread.java:619)
*/