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(); } }
啦啦啦
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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安装目录情况