您的位置:首页
Lock 从来就没有成功过
2017-11-23 23:10
274 查看
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(); } }
啦啦啦
相关文章推荐
- 详细信息 - 1: IOR 文件“/tmp/gconfd-root/lock/ior”没有成功打开
- OzymanDNS 使用——perl 5.22没有成功。。。
- 点击应用报App isn't installed,或者Run完并成功后应用没有在手机上安装标问题
- Facebook CEO扎克伯格:成功没有捷径
- android软件版本升级时,安装新版本后,没有出现安装成功界面或直接回到桌面
- 调用save()方法,页面显示保存成功,但是数据库中没有值的原因
- 没有一个是成功的故事
- PHP 博客文集 1 Veda原型 成功,唯有积累,没有奇迹
- 成功了!没有虚拟机,照样同时安装Windows和Linux
- V$LOCK视图显示TX锁没有对应的TM锁的几种可能现象
- 一个汇编语言的学生管理(当时没有做成功,现在也懒得改了,可以随便看看)
- 当/tmp下的文件,data下的文件,datalog下的没有删除,导致format或bin/start-all.sh不成功
- MySQL 事务没有提交导致 锁等待 Lock wait timeout exceeded
- Hadoop Yarn 集群 从节点的data node没有启动成功
- 李彦宏:百度的成功和谷歌退出没有必然联系
- 成功没有奇迹,只有轨迹!!!
- tomcat启动成功但是没有监听8080端口
- Application Loader 上传应用到AppStore 提示成功,但是iTunesConnect里却一直没有可以构建的版本
- Python 的 MySQLdb 模块插入数据没有成功与 autocommit(自动提交)的关系