Lock和Condition对象的使用

Condition的await()方法

void await()
    throws InterruptedException
导致当前线程等到发信号或interrupted

与此相关的锁Condition以原子方式释放,并且当前线程的线程调度目的就退出,一直处于休眠状态的四两件事发生对象

  • 一些其它线程调用signal()方法本Condition和当前线程碰巧被选择作为被唤醒线程; 要么
  • 一些其他的线程调用88452369060832方法Condition ; 要么
  • 一些其他线程interrupts当前线程,并中断线程挂起; 要么
  • 发生“ 虚假唤醒 ”。

在所有情况下,在此方法返回之前,当前线程必须重新获取与此条件相关的锁。 当线程返回时,它保证保持此锁。

实现两个线程交替执行的案例:

class Car{
    private Lock lock=new ReentrantLock();
    private Condition condition=lock.newCondition();
    private boolean waxOn=false;
    public void waxed(){
        lock.lock();
        try {
            waxOn=true;
            condition.signalAll();//唤醒所有等待线程
        } finally {
            lock.unlock();
        }
    }
    public void buffed(){
        lock.lock();
        try {
            waxOn=false;
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }
    public void waitForWaxing() throws InterruptedException{
        lock.lock();
        try {
            while(waxOn==false)
                condition.await();//挂起当前线程,释放锁
        } finally {
            lock.unlock();
        }
    }
    public void waitForBuffing() throws InterruptedException{
        lock.lock();
        try {
            while(waxOn==true)
                condition.await();
        } finally {
            lock.unlock();
        }
    }
}
class WaxOn implements Runnable{
    private Car car;
    public WaxOn(Car c){
        car=c;
    }

    @Override
    public void run() {
        try {
            while(!Thread.interrupted()){
                System.out.println("Wax on!");
                TimeUnit.MILLISECONDS.sleep(200);
                car.waxed();
                car.waitForBuffing();
            }
        } catch (InterruptedException e) {
            System.out.println("Exiting via interrupt");
        }
        System.out.println("Ending Wax On task");
    }
}
class WaxOff implements Runnable{
    private Car car;
    public WaxOff(Car c){
        car=c;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()){
                car.waitForWaxing();
                System.out.println("Wax Off!");
                TimeUnit.MILLISECONDS.sleep(200);
                car.buffed();
            }
        } catch (InterruptedException e) {
            System.out.println("Exiting via interrupt");
        }
        System.out.println("Ending Wax Off task");
    }
}
public class WaxOMatic2 {
    public static void main(String[] args) throws Exception{
        Car car=new Car();
        ExecutorService exec= Executors.newCachedThreadPool();
        exec.execute(new WaxOff(car));
        exec.execute(new WaxOn(car));
        TimeUnit.SECONDS.sleep(5);
        exec.shutdownNow();
    }
}
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Wax Off!
Wax on!
Exiting via interrupt
Ending Wax On task
Exiting via interrupt
Ending Wax Off task


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值