Condition

java.util.concurrent类库中提供Condition类来实现线程之间的协调。

调用Condition.await() 方法使线程等待,其他线程调用Condition.signal() 或 Condition.signalAll() 方法唤醒等待的线程。

  • 上锁类
public class TestObject {
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void first() {
        System.out.println("first is lock");
        lock.lock();
        try {
            System.out.println("first is running");
            condition.signalAll();
        } finally {
            System.out.println("first is unlock");
            lock.unlock();
        }
    }

    public void second() {
        System.out.println("second is lock");
        lock.lock();
        try {
            condition.await();
            System.out.println("second is running");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("second is unlock");
            lock.unlock();
        }
    }

    public void third() {
        System.out.println("third is lock");
        lock.lock();
        try {
            condition.await();
            System.out.println("third is running");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            System.out.println("third is unlock");
            lock.unlock();
        }
    }
}
  • 测试类
public class TestRun {

    private final static ExecutorService executorService = Executors.newCachedThreadPool();

    public static void main(String[] arg) {
        TestObject to = new TestObject();
        executorService.execute(() -> to.third());
        executorService.execute(() -> to.second());
        executorService.execute(() -> to.first());
    }
}
  • 输出结果
third is lock
second is lock
first is lock
first is running
first is unlock
third is running
third is unlock
second is running
second is unlock

备注:上述实例中,若first方法中改为condition.signal()则second不会被唤醒,需要在third方法的finally中增加condition.signal()才能唤醒second。

wait() & await()

  • wait()是Object类中的方法,await()是Condition类里面的方法
  • await的阻塞需要Lock中的Condition对象来触发
  • await会导致当前线程被阻塞,会释放锁,这点和wait是一样的
  • await使用signal唤醒;wait使用notify
  • await只能用于ReentrantLock锁中,wait一般用于Synchronized中

老污的猫
30 声望5 粉丝