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
【推荐】博客园2025新款「AI繁忙」系列T恤上架,前往周边小店选购
【推荐】凌霞软件回馈社区,携手博客园推出1Panel与Halo联合会员
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· 通过一个DEMO理解MCP(模型上下文协议)的生命周期
· MySQL下200GB大表备份,利用传输表空间解决停服发版表备份问题
· 记一次 .NET某固高运动卡测试 卡慢分析
· 微服务架构学习与思考:微服务拆分的原则
· 解锁.NET 9性能优化黑科技:从内存管理到Web性能的最全指南
· .net clr 8年才修复的BUG,你让我损失太多了
· .NET周刊【3月第5期 2025-03-30】
· 即时通信SSE和WebSocket对比
· 一个神奇的JS代码,让浏览器在新的空白标签页运行我们 HTML 代码(createObjectURL
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安装目录情况