Lock 从来就没有成功过

复制代码
package lime.thinkingInJava._021._005._003;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author : Lime
 * @Description :
 * @Remark :
 */
class Car{
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    private boolean waxOn = false;

    public void waxOn(){
        lock.lock();
        try{
            waxOn = true;
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }
    public void waxOff(){
        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 car) {
        this.car = car;
    }

    @Override
    public void run() {
        try {
            while (!Thread.interrupted()) {
                System.out.println("waxOn");
                TimeUnit.MILLISECONDS.sleep(200);
                car.waxOn();
                car.waitForBuffing();
            }
        } catch (InterruptedException e) {
        }
    }
}
class WaxOff implements Runnable{
    private Car car;

    public WaxOff(Car car) {
        this.car = car;
    }
    public void run(){
        try {
            while (!Thread.interrupted()) {
                car.waitForWaxing();
                System.out.println("waxOff");
                TimeUnit.MILLISECONDS.sleep(200);
                car.waxOff();
            }
        } catch (InterruptedException e) {
        }
    }
}
public class WaxOMatic2 {
    public static void main(String[] args) throws InterruptedException {
        ExecutorService exec = Executors.newCachedThreadPool();
        Car car = new Car();
        exec.execute(new WaxOff(car));
        exec.execute(new WaxOn(car));
        TimeUnit.SECONDS.sleep(3);
        exec.shutdownNow();
    }
}
复制代码

啦啦啦

posted @   limeOracle  阅读(124)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 字符编码:从基础到乱码解决
历史上的今天:
2016-11-23 linux 删除用户
2016-11-23 linux 查看所有用户
2016-11-23 ubuntu下添加/删除启动服务项
2016-11-23 Ubuntu中设置静态IP和DNS
2016-11-23 ubuntu如何卸载apt-get install安装的软件
2016-11-23 ubuntu mysql 导入外部sql文件
2016-11-23 ubuntu MySQL采用apt-get install安装目录情况
点击右上角即可分享
微信分享提示