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. 不要只看时钟,要效 ...
随机推荐
- ES6 迭代器和生成器
设计为了更高效的数据处理,避免过多for循环嵌套(代码复杂度,跟踪多个循环变量) 1. 迭代器: 为迭代过程设计的接口 所有的迭代器对象都有next()方法,每次调用都返回一个结果对象,对象有两个属性 ...
- Codeforces Round #443 (Div. 1) A. Short Program
A. Short Program link http://codeforces.com/contest/878/problem/A describe Petya learned a new progr ...
- 通过DCGAN进行生成花朵
环境是你要安装Keras和Tensorflow 先来个network.py,里面定义了生成器网络和鉴别器网络: # -*- coding: UTF-8 -*- """ D ...
- 词向量保存为txt
model.wv.save_word2vec_format('w2v_mod.txt',binary=False)
- Windows远程桌面(mstsc)不能复制粘贴的解决办法
原来通过mstsc远程连接Windows能够直接在本地和服务器之间复制.粘贴一些文字和文件,最近突然无法直接复制粘贴了,只能通过远程映射的本地磁盘方式交换文件,太为繁琐. 已经确认远程桌面连接选项“本 ...
- Caused by: java.security.InvalidKeyException: Illegal key size or default parameters
How to remove the key size restriction in Java JDK? Are you developing your beautiful application us ...
- Python常用的软件包
下面是Python开发常用的软件包. 名称 用途 安装命令 opengl sudo pip3 install PyOpenGL pyqtgraph GUI图形库 sudo pip3 instal ...
- centOS --- 安装最新版的node nodejs
对于 RHEL, CentOS or Fedora 系统,Node.js v8 LTS版本命令: curl –silent –location https://rpm.nodesource.com/s ...
- centos7 快速安装 mariadb(mysql)
从最新版本的linux系统开始,默认的是 Mariadb而不是mysql! 使用系统自带的repos安装很简单: yum install mariadb mariadb-server systemct ...
- Android如何实现茄子快传
Android如何实现茄子快传茄子快传是一款文件传输应用,相信大家都很熟悉这款应用,应该很多人用过用来文件的传输.它有两个核心的功能: 端到端的文件传输Web端的文件传输这两个核心的功能我们具体来分析 ...