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();
}
}
啦啦啦
Lock 从来就没有成功过的更多相关文章
- C# lock 为什么要设置成只读
首先给出MSDN的定义: lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互斥锁来实现的. 先来看看执行过程,代码示例如下: 假设线程A先执行, ...
- gitlab一键安装 (转)
原文地址:http://www.2cto.com/os/201411/353292.html 0 简介bitnami和gitlab bitnami BitNami是一个开源项目,该项目产生的开源软件包 ...
- centos6.5+jexus5.6.3+mono 3.10实践,让asp.net在linux上飞一会儿
备忘,这是给自己看的,用ubuntu server装mono 3.10老是卡在了编译libgdiplus上面,从来就没成功过,郁闷啊,零零散散搞了好几天,作罢.后来试了OpenSUSE 11很容易搞好 ...
- Oracle_集合
游标遍历select语句 set serveroutput on; declare type sp_test1_cursor is ref cursor; test1_cursor sp_test1_ ...
- 艰苦的RAW格式数据恢复之旅
艰苦的RAW格式数据恢复之旅 1.RAW 格式形成原因 2.RAW 格式的解决的方法 经验之谈: 1.RAW 格式形成原因 关于形成的原因,在网上搜索了下,千奇百怪的都有,就不一一诉说了,可是有果必有 ...
- webmagic学习-使用注解编写爬虫
写在前面: 官方文档:http://webmagic.io/docs/zh/posts/ch5-annotation/README.html WebMagic支持使用独有的注解风格编写一个爬虫,引入w ...
- 爆炸,解体,入侵,你想得到的你想不到的大BUG们
郑昀 创建于2017/9/29 最后更新于2017/10/6 提纲: 阿丽亚娜火箭的解体 阿波罗飞船的P01模式 德勤的Google+ 麻省理工的500英里邮件 又到了扶额兴叹的节气.(前文回顾:5年 ...
- Flarum轻量级论坛的安装
论坛作为互联网中的远古产物,经历了如QQ群.社区和贴吧等新兴社交工具的冲击,依然能够存在,肯定是有着不可替代的用处,像吾爱.远景等论坛依旧火热.一些博客主也喜欢自己搭建一个论坛作为用户聚集之地. 之前 ...
- 20165215 学习基础和c语言基础调查
学习基础和c语言基础调查 <做中学>读后感与技能学习心得 读后感 Don't watch the clock. Do what it does. Keep going. 不要只看时钟,要效 ...
随机推荐
- Microsoft.AspNet.Identity: UserID用整型数据表示, 而不是GUID
第一篇: 这个好像不太好 http://stackoverflow.com/questions/19553424/how-to-change-type-of-id-in-microsoft-aspn ...
- Linux命令之tar篇
作业一: 1) 将用户信息数据库文件和组信息数据库文件纵向合并为一个文件/1.txt(覆盖) [root@localhost ~]# cat /etc/passwd /etc/group > ...
- Codeforces899C Dividing the numbers(数论)
http://codeforces.com/problemset/problem/899/C tot为奇数时,绝对差为1:tot为偶数时,绝对差为0. 难点在于如何输出. #include<io ...
- .NET开源Protobuf-net组件修炼手册
一.前言 Protocol Buffer(简称Protobuf或PB) 是一个跨平台的消息交互协议,类似xml.json等 :别只会用Json和XML了,快来看看Google出品的Protocol B ...
- PL/SQL中查询Oracle大数(17位以上)时显示科学计数法的解决方法
PL/SQL查询时,如果Number(17)以上的大数,会显示为科学计数法 解决方法: TOOLS->PREFERENCES->WINDOW TYPE->SQL WINDOW下选中N ...
- UML建模——用例图(Use Case Diagram)
用例图主要用来描述角色以及角色与用例之间的连接关系.说明的是谁要使用系统,以及他们使用该系统可以做些什么.一个用例图包含了多个模型元素,如系统.参与者和用例,并且显示这些元素之间的各种关系,如泛化.关 ...
- Elastic{ON}参会随手记
Elastic{ON} 同事送了一张Elastic{ON}的票,因为我们的产品中用到的ELK全家桶,实话说用的体量还挺大的,因此非常想去参加这次的发布会. 7.0的新特性 上午的会议是来自总部的一名工 ...
- Translate Angular >=4 with ngx-translate and multiple modules
原文:https://medium.com/@lopesgon/translate-angular-4-with-ngx-translate-and-multiple-modules-7d9f0252 ...
- 网页出现400 Bad Request Request Header Or Cookie Too Large错误的解决方法
在开发项目过程中,突然遇到400 Bad Request Request Header Or Cookie Too Large的报错,我也是第一次出现这样的错误,感觉还是挺新奇的. 分析下出现错误的原 ...
- Lucene与Solr基础
SolrSelectTest 查询与删除 package com.snow.solr; import com.snow.bean.Product; import org.apache.solr.cli ...