本案例描述的是,给一辆汽车打蜡、抛光的场景。

Car 是一辆被打蜡抛光的汽车,扮演共享资源的角色。

WaxOnCommand 负责给汽车打蜡,打蜡时需要独占整部车,一次打一部分蜡,等待抛光,然后再打一部分蜡。

BuffCommand 负责给汽车抛光,抛光时需要独占整部车,每一次会将刚打上的蜡抛光,然后等待打蜡,然后再将刚打上的蜡抛光。

WaxOnCommand 、BuffCommand 分别由两个线程相互交替地执行:

  WaxOnCommand 打蜡、等待抛光,BuffCommand 抛光,等待打蜡;

  WaxOnCommand 打蜡、等待抛光,BuffCommand 抛光,等待打蜡;

  WaxOnCommand 打蜡、等待抛光,BuffCommand 抛光,等待打蜡;

  ......

CarWaxBuffDemo 演示这个场景。

下面是类之间的关系图。

由于 ExecutorServices 的使用和 Command Pattern 很相似,所以参照其 UML 绘制而成。

代码实现:

Car, 可以改变汽车的打蜡状态,可以等待打蜡状态的改变。输出内容到控制台时,会把当前执行线程一一同输出。

public class Car {

    private boolean isWaxed = false;

    public synchronized void waxed(){
isWaxed = true;
print(" car is waxed now ");
notifyAll();
} public synchronized void unwaxed(){
isWaxed = false;
print(" car is unwaxed now ");
notifyAll();
}public synchronized void waitingForReWax() throws InterruptedException{
while (isWaxed == true){
wait();
}
print(" OK to re-wax "); } public synchronized void waitingForReBuff() throws InterruptedException{
while (isWaxed == false){
wait();
}
print(" OK to re-Buff ");
} public void print(String msg){
System.out.println("[" + Thread.currentThread().getName() + " ] " + msg);
}
}

WaxonComman,给汽车打蜡,主要是两个动作:1.等待可以打蜡,2.打蜡。

import java.util.concurrent.TimeUnit;

public class WaxonCommand implements Runnable {

    private Car car;

    public WaxonCommand(Car car){
this.car = car;
} @Override
public void run() {
try {
while (true){
car.waitingForReWax(); print(" WaxOn take action ");
TimeUnit.MILLISECONDS.sleep(); car.waxed();
}
}catch (Exception e) {
e.printStackTrace();
} try {
TimeUnit.MILLISECONDS.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
} print(" Ending WaxOn action ");
} public void print(String msg){
System.out.println("[" + Thread.currentThread().getName() + " ] " + msg);
}
}

buffCommand, 给汽车抛光,主要两个动作:1.等待可以抛光,2. 抛光。

import java.util.concurrent.TimeUnit;

public class BuffCommand implements Runnable{

    private Car car;

    public BuffCommand(Car car){
this.car = car;
} @Override
public void run() {
try {
while (true){
car.waitingForReBuff(); print(" Buff take action ");
TimeUnit.MILLISECONDS.sleep(); car.unwaxed();
}
}catch (InterruptedException e) {
e.printStackTrace();
} try {
TimeUnit.MILLISECONDS.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
} print(" Ending Buff action "); } public void print(String msg){
System.out.println("[" + Thread.currentThread().getName() + " ] " + msg);
}
}

演示给汽车交替打蜡、抛光的场景。给 1 秒的时间,让 WaxonCommand / BuffComand 线程进行打蜡、抛光操作,1 秒后,中断他们的操作。

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit; public class CarWaxBuffDemo { public static void main() throws InterruptedException{
ExecutorService exec = Executors.newCachedThreadPool(); Car car = new Car();
exec.execute(new BuffCommand(car));
exec.execute(new WaxonCommand(car)); TimeUnit.SECONDS.sleep(1);
System.out.println(" executor stopping ");
exec.shutdownNow();
}
}

输出结果:

在被强制中断前,thread-1 和 thread-2 交替进行打蜡、抛光操作。

[pool-1-thread-1 ]  OK to re-wax
[pool-1-thread-1 ] WaxOn take action
[pool-1-thread-1 ] car is waxed now
[pool-1-thread-2 ] OK to re-Buff
[pool-1-thread-2 ] Buff take action
[pool-1-thread-2 ] car is unwaxed now
[pool-1-thread-1 ] OK to re-wax
[pool-1-thread-1 ] WaxOn take action
[pool-1-thread-1 ] car is waxed now
[pool-1-thread-2 ] OK to re-Buff
[pool-1-thread-2 ] Buff take action
[pool-1-thread-2 ] car is unwaxed now
[pool-1-thread-1 ] OK to re-wax
[pool-1-thread-1 ] WaxOn take action
[pool-1-thread-1 ] car is waxed now
[pool-1-thread-2 ] OK to re-Buff
[pool-1-thread-2 ] Buff take action
[pool-1-thread-2 ] car is unwaxed now
[pool-1-thread-1 ] OK to re-wax
[pool-1-thread-1 ] WaxOn take action
[pool-1-thread-1 ] car is waxed now
[pool-1-thread-2 ] OK to re-Buff
[pool-1-thread-2 ] Buff take action
[pool-1-thread-2 ] car is unwaxed now
[pool-1-thread-1 ] OK to re-wax
[pool-1-thread-1 ] WaxOn take action
[pool-1-thread-1 ] car is waxed now
[pool-1-thread-2 ] OK to re-Buff
[pool-1-thread-2 ] Buff take action
[pool-1-thread-2 ] car is unwaxed now
[pool-1-thread-1 ] OK to re-wax
[pool-1-thread-1 ] WaxOn take action
executor stopping
java.lang.InterruptedException
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Object.java:502)
at concurrencyCooperation.Car.waitingForReBuff(Car.java:29)
at concurrencyCooperation.BuffCommand.run(BuffCommand.java:17)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at java.lang.Thread.sleep(Thread.java:340)
at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386)
at concurrencyCooperation.WaxonCommand.run(WaxonCommand.java:20)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
[pool-1-thread-2 ] Ending Buff action
[pool-1-thread-1 ] Ending WaxOn action

参考资料 :

Page 857, wait() and notifyAll(), Thinking in Java

[Java Concurrent] 多线程合作 wait / notifyAll 的简单案例的更多相关文章

  1. [Java Concurrent] 多线程合作 producer-consumers / queue 的简单案例

    在多线程环境下,通过 BlockingQueue,实现生产者-消费者场景. Toast 被生产和消费的对象. ToastQueue 继承了 LinkedblockingQueue ,用于中间存储 To ...

  2. java基础之Socket编程概述以及简单案例

    概述: 用来实现网络互连的 不同的计算机上 运行的程序间 可以进行数据交互  也就是用来在不同的电脑间, 进行数据传输. 三大要素: IP地址: 设备(电脑,手机,ipad)在网络中的唯一标识. 组成 ...

  3. Java Tread多线程(0)一个简单的多线程实例

    作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39341887 本文演示,一个简单的多线程实例,并简单分析一下线程. 编程多 ...

  4. Java Tread多线程(1)实现Runnable接口

    作者 : 卿笃军 原文地址:http://blog.csdn.net/qingdujun/article/details/39347245 本文演示,Tread多线程实现Runnable接口,以及简单 ...

  5. Java:多线程,java.util.concurrent.atomic包之AtomicInteger/AtomicLong用法

    1. 背景 java.util.concurrent.atomic这个包是非常实用,解决了我们以前自己写一个同步方法来实现类似于自增长字段的问题. 在Java语言中,增量操作符(++)不是原子的,也就 ...

  6. Java的多线程 简单入门

    Java的多线程 简单入门 首先能够先搞清楚什么是程序.进程.线程,以及它们之间的关系: 定义: 一 程序仅仅是一组指令的有序集合.它是静态的 二 进程是具有一定独立功能的程序关于某个数据集合上的一次 ...

  7. Java的多线程机制系列:(一)总述及基础概念

    前言 这一系列多线程的文章,一方面是个人对Java现有的多线程机制的学习和记录,另一方面是希望能给不熟悉Java多线程机制.或有一定基础但理解还不够深的读者一个比较全面的介绍,旨在使读者对Java的多 ...

  8. java 并发多线程显式锁概念简介 什么是显式锁 多线程下篇(一)

    目前对于同步,仅仅介绍了一个关键字synchronized,可以用于保证线程同步的原子性.可见性.有序性 对于synchronized关键字,对于静态方法默认是以该类的class对象作为锁,对于实例方 ...

  9. Java的多线程实现生产/消费模式

    Java的多线程实现生产/消费模式 在Java的多线程中,我们经常使用某个Java对象的wait(),notify()以及notifyAll() 方法实现多线程的通讯,今天就使用Java的多线程实现生 ...

随机推荐

  1. 试答卓同学的 iOS 面试题

    卓同学昨天写了一篇文章<4道过滤菜鸟的iOS面试题>.我手痒决定默写一个参考答案.后来发现不认真回答被大家喷成狗,所以决定积极改造,重新做人.下面就是修编之后的答案. 1. struct和 ...

  2. python中的generator, iterator, iterabel

    先来看看如果遇到一个对象,如何判断其是否是这三种类型: from types import GeneratorType from collectiuons import Iterable, Itera ...

  3. 用angular来思考问题How do I “think in AngularJS” if I have a jQuery background?

    [翻译]How do I “think in AngularJS” if I have a jQuery background? 1. 不要先设计页面,然后再使用DOM操作来改变它的展现 在jQuer ...

  4. Excel:您尝试打开的文件的格式与文件扩展名指定的格式不一致

    报错信息: 打开文件时提示"您尝试打开的文件xxx.xls的格式与文件扩展名指定的格式不一致.打开文件前请验证文件没有损坏且来源可信.是否立即打开该文件?",卸载Office 20 ...

  5. Oracle 用户(user)和模式(schema)的区别

    概述: (一)什么Oracle叫用户(user): A user is a name defined in the database that can connect to and access ob ...

  6. 使用ol,添加图书销售排行榜

    如果想在网页中展示有前后顺序的信息列表,怎么办呢?如,当当网上的书籍热卖排行榜,如下图所示. 这类信息展示就可以使用<ol>标签来制作有序列表来展示. 语法: <ol> < ...

  7. 四、C#方法和参数

    方法是一种组合一系列语句以执行一个特定操作或计算一个特殊结果的方式. 它能够为构成程序的语句提供更好的结构和组织.   在面向对象的语言中,方法总是和类关联在一起的,我们用类将相关的方法分为一组. 方 ...

  8. (转载)用css来实现十字的布局

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. 初涉JavaScript模式 (11) : 模块模式

    引子 这篇算是对第9篇中内容的发散和补充,当时我只是把模块模式中的一些内容简单的归为函数篇中去,在北川的提醒下,我才发觉这是非常不严谨的,于是我把这些内容拎出来,这就是这篇的由来. 什么是模块模式 在 ...

  10. 好用的JQ图片特效jquery-poptrox-popup-galleries

    jQuery Poptrox – Popup galleries     Rate this (1 Vote) Download   Demo jQuery Poptrox Adds popup ga ...