/*:“运货司机”、“装运工”和“仓库管理员”。
* 要求线程“运货司机”占有CPU资源后立刻联合线程“装运工”,
* 也就是让“运货司机”一直等到“装运工”完成工作后才能开车,
* 而“装运工”占有CPU资源后立刻联合线程“仓库管理员”,
* 也就是让“装运工”一直等到“仓库管理员”打开仓库后才能开始搬运货物。*/
程序最后运行结果大致如下:司机正在等待装运工完成工作
装运工等仓库管理员打开仓库
仓库管理员正准备打开仓库,请等。。
仓库管理员打开仓库
装运工搬运货物,请等
货运司机开车
public class TestThread extends Thread
{
static Work work = new Work();
public TestThread(Work work)
{
this.work = work;
}
public void run()
{
if(Thread.currentThread().getName().equals("driver"))
work.driver();
else
if(Thread.currentThread().getName().equals("worker"))
work.worker();
else
work.manager();
}
public static void main(String[] args)
{
TestThread th1 = new TestThread(work);
th1.setName("driver");
th1.start();
TestThread th2 = new TestThread(work);
th2.setName("worker");
th2.start();
TestThread th3 = new TestThread(work);
th3.setName("manager");
th3.start();
}
}
class Work
{
boolean workerWait = true ,managerWait = true,driverWait = true,workerReWait = true;;
public synchronized void driver()
{
System.out.println("司机正在等待装运工完成工作");
workerWait = false;
notify();
if(driverWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(driverWait);
System.out.println("货运司机开车");
System.out.println(driverWait);
}
public synchronized void worker()
{
if(workerWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("装运工等仓库管理员打开仓库");
managerWait = false;
notify();
if(workerReWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("装运工搬运货物,请等");
}
public synchronized void manager()
{
if(managerWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("仓库管理员正准备打开仓库,请等");
try{Thread.sleep(3000);}catch(Exception e){e.printStackTrace();}
System.out.println("仓库管理员打开仓库");
workerReWait = false;
notify();
try{Thread.sleep(3000);}catch(Exception e){e.printStackTrace();}
driverWait = false;
notify();
}
}
达不到效果,调试的时候我发现有这个结果:司机正在等待装运工完成工作
装运工等仓库管理员打开仓库
true
货运司机开车
true
为什么driverWait的值为true,而司机线程不wait()?
3个线程如何notify指定的其中一个线程?
谁能做下这个题就更好了,感激不尽!
* 要求线程“运货司机”占有CPU资源后立刻联合线程“装运工”,
* 也就是让“运货司机”一直等到“装运工”完成工作后才能开车,
* 而“装运工”占有CPU资源后立刻联合线程“仓库管理员”,
* 也就是让“装运工”一直等到“仓库管理员”打开仓库后才能开始搬运货物。*/
程序最后运行结果大致如下:司机正在等待装运工完成工作
装运工等仓库管理员打开仓库
仓库管理员正准备打开仓库,请等。。
仓库管理员打开仓库
装运工搬运货物,请等
货运司机开车
public class TestThread extends Thread
{
static Work work = new Work();
public TestThread(Work work)
{
this.work = work;
}
public void run()
{
if(Thread.currentThread().getName().equals("driver"))
work.driver();
else
if(Thread.currentThread().getName().equals("worker"))
work.worker();
else
work.manager();
}
public static void main(String[] args)
{
TestThread th1 = new TestThread(work);
th1.setName("driver");
th1.start();
TestThread th2 = new TestThread(work);
th2.setName("worker");
th2.start();
TestThread th3 = new TestThread(work);
th3.setName("manager");
th3.start();
}
}
class Work
{
boolean workerWait = true ,managerWait = true,driverWait = true,workerReWait = true;;
public synchronized void driver()
{
System.out.println("司机正在等待装运工完成工作");
workerWait = false;
notify();
if(driverWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(driverWait);
System.out.println("货运司机开车");
System.out.println(driverWait);
}
public synchronized void worker()
{
if(workerWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("装运工等仓库管理员打开仓库");
managerWait = false;
notify();
if(workerReWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("装运工搬运货物,请等");
}
public synchronized void manager()
{
if(managerWait)
try
{
wait();
} catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("仓库管理员正准备打开仓库,请等");
try{Thread.sleep(3000);}catch(Exception e){e.printStackTrace();}
System.out.println("仓库管理员打开仓库");
workerReWait = false;
notify();
try{Thread.sleep(3000);}catch(Exception e){e.printStackTrace();}
driverWait = false;
notify();
}
}
达不到效果,调试的时候我发现有这个结果:司机正在等待装运工完成工作
装运工等仓库管理员打开仓库
true
货运司机开车
true
为什么driverWait的值为true,而司机线程不wait()?
3个线程如何notify指定的其中一个线程?
谁能做下这个题就更好了,感激不尽!
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.TimeUnit;public class ThreadTest {
public static void main(String[] args) {
BlockingQueue queue_1=new LinkedBlockingQueue(),
queue_2=new LinkedBlockingQueue();
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new StorageMangaer(queue_1));
exec.execute(new packworker(queue_1,queue_2));
exec.execute(new Driver(queue_2));
exec.shutdown();
}}
class Goods{
private String status;
private int id;
public Goods(int id) {
super();
this.id = id;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
class StorageMangaer implements Runnable{
private BlockingQueue queue;
private int flag=0;
public StorageMangaer(BlockingQueue queue){
this.queue=queue;
}
@Override
public void run() {
try {
while(!Thread.interrupted()){
TimeUnit.SECONDS.sleep(1);
Goods good=new Goods(flag++);
good.setStatus("StorageMangaer has opened the gate,and take up the good_"+good.getId());
System.out.println(good.getStatus());
queue.put(good);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class packworker implements Runnable{
private BlockingQueue queue_1,queue_2;
public packworker(BlockingQueue queue_1,BlockingQueue queue_2){
this.queue_1=queue_1;
this.queue_2=queue_2;
}
@Override
public void run() {
try {
while(!Thread.interrupted()){
TimeUnit.SECONDS.sleep(1);
Goods good=(Goods)queue_1.take();
good.setStatus("packworker has put the good_"+good.getId()+"on truck");
System.out.println(good.getStatus());
queue_2.put(good);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Driver implements Runnable{
private BlockingQueue queue_1;
public Driver(BlockingQueue queue_1){
this.queue_1=queue_1;
}
@Override
public void run() {
try {
while(!Thread.interrupted()){
TimeUnit.SECONDS.sleep(1);
Goods good=(Goods)queue_1.take();
good.setStatus("Driver has transport the good_"+good.getId()+"by truck");
System.out.println(good.getStatus());
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试结果:StorageMangaer has opened the gate,and take up the good_0
packworker has put the good_0on truck
Driver has transport the good_0by truck
StorageMangaer has opened the gate,and take up the good_1
packworker has put the good_1on truck
Driver has transport the good_1by truck
StorageMangaer has opened the gate,and take up the good_2
packworker has put the good_2on truck
Driver has transport the good_2by truck
StorageMangaer has opened the gate,and take up the good_3
packworker has put the good_3on truck
Driver has transport the good_3by truck
StorageMangaer has opened the gate,and take up the good_4
packworker has put the good_4on truck
Driver has transport the good_4by truck
public synchronized void driver(){
notifyAll();
}
public synchronized void work(){
notifyAll();
}
public synchronized void manage(){
notifyAll();
}
public synchronized void waitForWoking() throws InterruptedException{
wait();
}
public synchronized void waitForManaging() throws InterruptedException{
wait();
}}司机类:import java.util.concurrent.TimeUnit;
public class Driver implements Runnable{
private Doing doing;
public Driver(Doing doing){
this.doing=doing;
}
public void run() {
try{
if(!Thread.interrupted()){
System.out.println("司机正在等待装运工完成工作");
TimeUnit.MILLISECONDS.sleep(200);
doing.driver();
doing.waitForWoking();
}
}catch(InterruptedException e){
System.out.println(" exiting Interrupte");
}
System.out.println(" 货运司机开车");
}
}工人类:import java.util.concurrent.TimeUnit;
public class Work implements Runnable{
private Doing doing;
public Work(Doing doing){
this.doing=doing;
}
public void run() {
try{
if(!Thread.interrupted()){
System.out.println("装运工等仓库管理员打开仓库");
TimeUnit.MILLISECONDS.sleep(200);
doing.work();
doing.waitForManaging();
}
}catch(InterruptedException e){
System.out.println(" exiting Interrupted");
}
System.out.println("装运工搬运货物,请等 ");
}
}管理人员类:import java.util.concurrent.TimeUnit;
public class Manage implements Runnable{
private Doing doing;
public Manage(Doing doing){
this.doing=doing;
}
public void run() {
try{
if(!Thread.interrupted()){
System.out.println("仓库管理员正准备打开仓库,请等。。");
TimeUnit.MILLISECONDS.sleep(200);
System.out.println("仓库管理员打开仓库");
doing.manage();
}
}catch(InterruptedException e){
System.out.println(" exiting Interrupted");
}
}
}测试类:import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class Test {
public static void main(String[] args) throws InterruptedException {
Doing doing=new Doing();
ExecutorService exec=Executors.newCachedThreadPool();
exec.execute(new Driver(doing));
exec.execute(new Work(doing));
exec.execute(new Manage(doing));
TimeUnit.SECONDS.sleep(5);
exec.shutdown();
}
}
{
public static void main(String[] args)
{
Thread siji=new Thread(new sijiTread());
siji.start();
}
}
class sijiTread implements Runnable
{
public void run()
{
System.out.println("司机正在等待装运工完成工作");
Thread zhuangyun=new Thread(new zhuangyunTread());
zhuangyun.start();
try{
Thread.sleep(1000);
System.out.println("货运司机开车");
}catch(Exception e)
{
e.printStackTrace();
}
}
}class zhuangyunTread implements Runnable
{
public void run()
{
System.out.println("装运工等仓库管理员打开仓库");
Thread cangku=new Thread(new cangkuThread());
cangku.start();
try{
Thread.sleep(500);
System.out.println("装运工搬运货物,请等");
}catch(Exception e)
{
e.printStackTrace();
}
}
}class cangkuThread implements Runnable
{
public void run()
{
System.out.println("仓库管理员正准备打开仓库,请等。。 ");
try{
Thread.sleep(200);
System.out.println("仓库管理员打开仓库 ");
}catch(Exception e)
{
e.printStackTrace();
}
}
}运行结果:司机正在等待装运工完成工作
装运工等仓库管理员打开仓库
仓库管理员正准备打开仓库,请等。。
仓库管理员打开仓库
装运工搬运货物,请等
货运司机开车
1,大概看了下程序,共享资源应该是Work,那么所有的wait(),notify()应该都是针对Work的,也就是应该是work.wait(),work.notify()。
用在餐馆里吃饭打比方吧。
wait(): 小明到餐厅点了一盘(青菜),点了之后师傅要先炒好了(青菜)才能吃。所以小明先等(青菜),就是(青菜).wait()。然后你就加到了(等待青菜客户名单)里。
notify()/notifyall():菜炒好了,师傅一看(青菜客户名单),有三个人在等啊,谁先到我忘了,那就随便叫一个人。于是师傅在窗口叫“小明,你的(青菜菜)好了快点来拿”。看到了么?整个过程都是以---青菜---中心。2,Work里面的flag的设置没怎么明白,楼主的意思是,司机通知搬运工,我到了快去给我搬货。然后搬运工再跑到仓管员处: 我来搬货,快给我开下门?3,程序结构的话,可以参考其他几位上传的代码。拙见-_-