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 从来就没有成功过的更多相关文章

  1. C# lock 为什么要设置成只读

    首先给出MSDN的定义: lock 关键字可以用来确保代码块完成运行,而不会被其他线程中断.这是通过在代码块运行期间为给定对象获取互斥锁来实现的. 先来看看执行过程,代码示例如下: 假设线程A先执行, ...

  2. gitlab一键安装 (转)

    原文地址:http://www.2cto.com/os/201411/353292.html 0 简介bitnami和gitlab bitnami BitNami是一个开源项目,该项目产生的开源软件包 ...

  3. centos6.5+jexus5.6.3+mono 3.10实践,让asp.net在linux上飞一会儿

    备忘,这是给自己看的,用ubuntu server装mono 3.10老是卡在了编译libgdiplus上面,从来就没成功过,郁闷啊,零零散散搞了好几天,作罢.后来试了OpenSUSE 11很容易搞好 ...

  4. Oracle_集合

    游标遍历select语句 set serveroutput on; declare type sp_test1_cursor is ref cursor; test1_cursor sp_test1_ ...

  5. 艰苦的RAW格式数据恢复之旅

    艰苦的RAW格式数据恢复之旅 1.RAW 格式形成原因 2.RAW 格式的解决的方法 经验之谈: 1.RAW 格式形成原因 关于形成的原因,在网上搜索了下,千奇百怪的都有,就不一一诉说了,可是有果必有 ...

  6. webmagic学习-使用注解编写爬虫

    写在前面: 官方文档:http://webmagic.io/docs/zh/posts/ch5-annotation/README.html WebMagic支持使用独有的注解风格编写一个爬虫,引入w ...

  7. 爆炸,解体,入侵,你想得到的你想不到的大BUG们

    郑昀 创建于2017/9/29 最后更新于2017/10/6 提纲: 阿丽亚娜火箭的解体 阿波罗飞船的P01模式 德勤的Google+ 麻省理工的500英里邮件 又到了扶额兴叹的节气.(前文回顾:5年 ...

  8. Flarum轻量级论坛的安装

    论坛作为互联网中的远古产物,经历了如QQ群.社区和贴吧等新兴社交工具的冲击,依然能够存在,肯定是有着不可替代的用处,像吾爱.远景等论坛依旧火热.一些博客主也喜欢自己搭建一个论坛作为用户聚集之地. 之前 ...

  9. 20165215 学习基础和c语言基础调查

    学习基础和c语言基础调查 <做中学>读后感与技能学习心得 读后感 Don't watch the clock. Do what it does. Keep going. 不要只看时钟,要效 ...

随机推荐

  1. ajax请求json中的数据

    在这里不多说,直接可以运行代码看效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta ...

  2. word2vec skip-gram系列2

    Word2Vec的CBOW模型和Skip-gram模型 故事先从NNLM模型说起,网络结构图如下图所示,接下来先具体阐述下这个网络, 输入是个one-hot representation的向量.比如你 ...

  3. 使用JfreeChart生成图表遇到的问题

    生成的图片不显示 需要在web.xml中配置一个指定的Servlet <servlet> <servlet-name>DisplayChart</servlet-name ...

  4. [Java代码] Java用pinyin4j根据汉语获取各种格式和需求的拼音

    pinyin4j是一个功能强悍的汉语拼音工具包,主要是从汉语获取各种格式和需求的拼音,功能强悍,下面看看如何使用pinyin4j.下面介绍用pinyin4j来做的一个根据汉语获取各种格式和需求的拼音d ...

  5. java static learning

    /*static learning */ class one { public int a = 5; static { // 类加载时就运行代码码 System.out.println("s ...

  6. java解决手机上传竖拍照片旋转90\180\270度问题

    <dependency> <groupId>com.drewnoakes</groupId> <artifactId>metadata-extracto ...

  7. win32下使用相对exe文件的绝对路径资源

    在使用VC++进行开发时,如果按F5进行Debug时,当前相对资源是相对工程的vcxproj的文件夹目录,而直接双击运行exe时,资源是相对exe的文件夹目录.为了兼容这二者,最好使用绝对路径,这样无 ...

  8. Css3 实现循环留言滚动效果(一)

    一.常见留言滚动效果示例 html代码 <div class="runList"> <div class="runitem"> < ...

  9. 00、Word Count

    1.开发环境 1.eclipse-jee-neon-3 2.scala-ide:http://download.scala-ide.org/sdk/lithium/e46/scala212/stabl ...

  10. 通过IIS操作修改服务器文件没有权限的解决办法

    问题描述:通过部署在IIS上的程序去操做文件(比如删除.旋转图片等)时,在本地执行没有问题,但是部署到服务器上提示“没有权限”.解决方法:找到你需要操作的文件的根文件夹,右键点击属性 选择“安全”选项 ...