经典的线程协作例子
2014-09-26 21:44
211 查看
两个过程:一个是将蜡图到车上,一个是抛光它。抛光任务在图蜡任务完成之前,是不能执行工作的,而图蜡任务在涂另一层蜡之前,必须等抛光任务完成。
这里使用wait()和notifyAll()挂起和重新启动这些任务。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//打蜡工人
class Paintor implements Runnable{
String name;
Car c1;
Paintor(String n,Car c){
c1 = c;
name = n;
}
@Override
public void run() {
try {
c1.paint(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//抛光工人
class Bufffer implements Runnable{
String name;
Car c1;
Bufffer(String n,Car c){
c1 = c;
name = n;
}
@Override
public void run() {
try {
c1.buff(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Car {
private boolean isPainted = false;
//打蜡
public synchronized void paint(String worker) throws InterruptedException{
while(isPainted){
System.out.println(worker +" is waiting to paint");
wait();//还没抛光,去休息,把工作位置让出
}
System.out.println(worker +" is painting");
isPainted = true;
notifyAll();
}
//抛光
public synchronized void buff(String worker) throws InterruptedException{
while(!isPainted){
System.out.println(worker +" is waiting to buff");
wait();//还没打蜡,去休息,把工作位置让出
}
System.out.println(worker +" is buffing");
isPainted = false;
notifyAll();//所有正在休息的工人出来排队准备干活
}
public static void main(String[] args) {
final Car car1 = new Car();
ExecutorService es = Executors.newCachedThreadPool();
for(int i=1;i<=10;i++){//创建10个打蜡工人,10个抛光工人
es.submit(new Paintor("Painter"+i,car1));
es.submit(new Bufffer("Bufffer"+i,car1));
}
es.shutdown();
}
}
==================================
某次运行结果:
Painter1 is painting
Painter2 is waiting to paint
Bufffer1 is buffing
Painter2 is painting
Painter3 is waiting to paint
Painter4 is waiting to paint
Painter5 is waiting to paint
Bufffer5 is buffing
Painter5 is painting
Painter4 is waiting to paint
Painter3 is waiting to paint
Bufffer2 is buffing
Painter3 is painting
Painter4 is waiting to paint
Bufffer9 is buffing
Painter7 is painting
Painter4 is waiting to paint
Painter10 is waiting to paint
Painter6 is waiting to paint
Bufffer6 is buffing
Painter6 is painting
Painter10 is waiting to paint
Painter4 is waiting to paint
Painter8 is waiting to paint
Painter9 is waiting to paint
Bufffer3 is buffing
Painter9 is painting
Painter8 is waiting to paint
Painter4 is waiting to paint
Bufffer4 is buffing
Bufffer7 is waiting to buff
Bufffer8 is waiting to buff
Bufffer10 is waiting to buff
Painter10 is painting
Bufffer10 is buffing
Bufffer8 is waiting to buff
Bufffer7 is waiting to buff
Painter4 is painting
Painter8 is waiting to paint
Bufffer7 is buffing
Bufffer8 is waiting to buff
Painter8 is painting
Bufffer8 is buffing
(我的个人博客:http://www.junhong.me/)
这里使用wait()和notifyAll()挂起和重新启动这些任务。
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
//打蜡工人
class Paintor implements Runnable{
String name;
Car c1;
Paintor(String n,Car c){
c1 = c;
name = n;
}
@Override
public void run() {
try {
c1.paint(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
//抛光工人
class Bufffer implements Runnable{
String name;
Car c1;
Bufffer(String n,Car c){
c1 = c;
name = n;
}
@Override
public void run() {
try {
c1.buff(name);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class Car {
private boolean isPainted = false;
//打蜡
public synchronized void paint(String worker) throws InterruptedException{
while(isPainted){
System.out.println(worker +" is waiting to paint");
wait();//还没抛光,去休息,把工作位置让出
}
System.out.println(worker +" is painting");
isPainted = true;
notifyAll();
}
//抛光
public synchronized void buff(String worker) throws InterruptedException{
while(!isPainted){
System.out.println(worker +" is waiting to buff");
wait();//还没打蜡,去休息,把工作位置让出
}
System.out.println(worker +" is buffing");
isPainted = false;
notifyAll();//所有正在休息的工人出来排队准备干活
}
public static void main(String[] args) {
final Car car1 = new Car();
ExecutorService es = Executors.newCachedThreadPool();
for(int i=1;i<=10;i++){//创建10个打蜡工人,10个抛光工人
es.submit(new Paintor("Painter"+i,car1));
es.submit(new Bufffer("Bufffer"+i,car1));
}
es.shutdown();
}
}
==================================
某次运行结果:
Painter1 is painting
Painter2 is waiting to paint
Bufffer1 is buffing
Painter2 is painting
Painter3 is waiting to paint
Painter4 is waiting to paint
Painter5 is waiting to paint
Bufffer5 is buffing
Painter5 is painting
Painter4 is waiting to paint
Painter3 is waiting to paint
Bufffer2 is buffing
Painter3 is painting
Painter4 is waiting to paint
Bufffer9 is buffing
Painter7 is painting
Painter4 is waiting to paint
Painter10 is waiting to paint
Painter6 is waiting to paint
Bufffer6 is buffing
Painter6 is painting
Painter10 is waiting to paint
Painter4 is waiting to paint
Painter8 is waiting to paint
Painter9 is waiting to paint
Bufffer3 is buffing
Painter9 is painting
Painter8 is waiting to paint
Painter4 is waiting to paint
Bufffer4 is buffing
Bufffer7 is waiting to buff
Bufffer8 is waiting to buff
Bufffer10 is waiting to buff
Painter10 is painting
Bufffer10 is buffing
Bufffer8 is waiting to buff
Bufffer7 is waiting to buff
Painter4 is painting
Painter8 is waiting to paint
Bufffer7 is buffing
Bufffer8 is waiting to buff
Painter8 is painting
Bufffer8 is buffing
(我的个人博客:http://www.junhong.me/)
相关文章推荐
- 单个线程唤醒多个线程的经典例子(加入了注释)
- JavaScript异步和单线程经典的例子setTimeout
- [原创][Java]由一个Java编程思想书中线程间协作的例子想到的
- java线程协作,经典生产者/消费者模式(二、Lock和Condition)
- Java_线程—经典的例子:生产者和消费者
- Java线程学习经典例子-读写者演示
- Java线程学习经典例子-读写者演示
- java线程协作,经典生产者/消费者模式(一、synchronized互斥)
- Java_线程—经典的例子:售票
- java 线程thread 协作 经典基础篇
- Java线程学习经典例子-读写者演示
- Windows 窗体编程 如何:使用后台线程搜索文件 (线程经典例子)
- 一个简单的C#多线程间同步的例子
- 线程安全1:定义及例子
- VC ++ 利用系统函数 QueueUserWorkItem 实现线程例子
- 一个经典例子让你彻彻底底理解java回调机制
- 线程间协作:wait、notify、notifyAll
- Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
- 一个调试、利用msdn的经典例子
- oracle中与分组函数有关的多表查询的经典例子