Thingking in Java学习笔记 wait() notifyAll()
来源:互联网 发布:淘宝卖家服务市场 编辑:程序博客网 时间:2024/05/02 01:31
共享资源是汽车Car,需要不断的打蜡waxed()和抛光buffed(),waxed()要等待buffed()完成,buffed()又要等待waxed()完成
<pre name="code" class="java">package com.test.concurrent;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.TimeUnit;public class WaxOMatic {public static void main(String[] args) throws InterruptedException {// TODO Auto-generated method stubCar car=new Car();ExecutorService exec=Executors.newCachedThreadPool();exec.execute(new WaxOn(car));exec.execute(new WaxOff(car));TimeUnit.SECONDS.sleep(1);System.out.println("trying to terminate all threads!!!!!");exec.shutdownNow();}}class Car{private boolean waxOn=false;public synchronized void waxed(){//打蜡System.out.println("waxOn!");waxOn=true;notifyAll();}public synchronized void buffed(){//抛光System.out.println("waxOff!");waxOn=false;notifyAll();}public synchronized void waitingForWaxing() throws InterruptedException{while(false==waxOn){System.out.println("waiting for Waxing!");wait();}}public synchronized void waitingForBufferring() throws InterruptedException{while(true==waxOn){System.out.println("waiting for Bufferring!");wait();}}}class WaxOn implements Runnable{private Car car;public WaxOn(Car c){car=c;}@Overridepublic void run(){try{while(!Thread.interrupted()){car.waxed();TimeUnit.MILLISECONDS.sleep(200);car.waitingForBufferring();}}catch(InterruptedException e){System.out.println("waxed interrupt exception");}System.out.println("waxed car end-------");}}class WaxOff implements Runnable{private Car car;public WaxOff(Car c){car=c;}@Overridepublic void run(){try{while(!Thread.interrupted()){car.waitingForWaxing();car.buffed();TimeUnit.MILLISECONDS.sleep(200);}}catch(InterruptedException e){System.out.println("buffed interrupt exception");}System.out.println("buffed car end-------");}}
输出:
waxOn!
waxOff!
waxOn!
waxOff!
waxOn!
waxOff!
waxOn!
waxOff!
waiting for Waxing!
waxOn!
waxOff!
trying to terminate all threads!!!!!
buffered car end-------
waxed car end-------
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:838]
0 0
- Thingking in Java学习笔记 wait() notifyAll()
- Thingking in Java学习笔记
- java并发学习笔记(一):wait() notifyAll() 生产者 消费者
- wait(), notify() and notifyAll() in Java
- java notify() notifyAll() wait()方法学习
- thingking in Java 字符串
- thingking in Java 泛型
- thingking in Java 数组
- thingking in Java 注解
- thingking in Java 并发
- java wait notify notifyall
- Java 多线程学习笔记 线程通讯 wait notify notifyAll的理解
- wait(), notify() and notifyAll() in Java - A tutorial
- thingking in java读书笔记3
- thingking in java 读书笔记---RTTI
- Thingking in Java 注册工厂
- thingking in Java 类型信息
- thingking in Java 枚举类型
- (四)Spark源码理解之BlockManager---part2
- op 整流 不适用虚短 一例 分析
- 【工作流前进之路】Activiti第一步:介绍+实例
- 黑马程序员_毕向东_Java基础视频教程第06天
- qt使用ffmpeg显示rtsp视频流
- Thingking in Java学习笔记 wait() notifyAll()
- php面试部分(二)
- 黑马程序员——Android程序运行过程
- 关键字:android,android开发,下划线,中划线
- Leetcode26-Remove Duplicates from Sorted Array
- Fields属性查询
- Cygwin NDK 安装配置详解
- HashMap源码解析
- 读懂卷积神经网络CNN