Condition

condition.await()

condition.signalAll()

package concurrent._Condition;

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

public class Demo {
    public static void main(String[] args) throws InterruptedException {

        ReentrantLock lock = new ReentrantLock();
        //Condition 类是AQS的一个内部类
        Condition condition = lock.newCondition();

        ThreadA a1= new ThreadA("a1",lock,condition);
        ThreadA a2 = new ThreadA("a2",lock,condition);
        ThreadA a3 = new ThreadA("a3",lock,condition);
        ThreadB b = new ThreadB("b",lock,condition);
        a1.start();
        a2.start();
        a3.start();
        Thread.sleep(500);
        b.start();

    }


}
class ThreadA extends  Thread{
    private Lock lock;
    private Condition condition;
    ThreadA(String name,Lock lock,Condition condition){
        super(name);
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        String threadName = this.getName();
        lock.lock();
        System.out.println(threadName + "已经获得锁");
        try {
            System.out.println(threadName + "等待一个条件");
            condition.await();
            System.out.println(threadName + "从挂起状态醒来");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {

            //System.out.println(threadName + "提供了一个条件");
            //condition.signal();
            System.out.println(threadName + "释放了锁");
            lock.unlock();
        }
    }
}

class ThreadB extends  Thread{
    private Lock lock;
    private Condition condition;
    ThreadB(String name,Lock lock,Condition condition){
        super(name);
        this.lock = lock;
        this.condition = condition;
    }

    @Override
    public void run() {
        String threadName = this.getName();
        lock.lock();
        System.out.println(threadName + "已经获得锁");
        System.out.println(threadName + "提供一个条件");
        condition.signalAll();
        System.out.println(threadName + "释放了锁");
        lock.unlock();
    }
}

结果显示:

a1已经获得锁
a1等待一个条件
a2已经获得锁
a2等待一个条件
a3已经获得锁
a3等待一个条件
b已经获得锁
b提供一个条件
b释放了锁
a1从挂起状态醒来
a1释放了锁
a2从挂起状态醒来
a2释放了锁
a3从挂起状态醒来
a3释放了锁

 

 

将ThreadB中改为唤起一个已经挂起的进程

将ThradA中改为每次释放锁之前唤起一个已经挂起线程

ThreadA

finally {

            System.out.println(threadName + "提供了一个条件");
            condition.signal();
            System.out.println(threadName + "释放了锁");
            lock.unlock();
        }

ThreadB

public void run() {
        String threadName = this.getName();
        lock.lock();
        System.out.println(threadName + "已经获得锁");
        System.out.println(threadName + "提供一个条件");
        condition.signal();
        System.out.println(threadName + "释放了锁");
        lock.unlock();
    }

结果:

a1已经获得锁
a1等待一个条件
a3已经获得锁
a3等待一个条件
a2已经获得锁
a2等待一个条件
b已经获得锁
b提供一个条件
b释放了锁
a1从挂起状态醒来
a1提供了一个条件
a1释放了锁
a3从挂起状态醒来
a3提供了一个条件
a3释放了锁
a2从挂起状态醒来
a2提供了一个条件
a2释放了锁

 

posted @ 2018-11-26 10:49  式微胡不归  阅读(657)  评论(0编辑  收藏  举报