Java 多线程同步问题的探究(四、协作,互斥下的协作——Java多线程协作(wait、notify、notifyAll))
2010-05-14 00:00:00 来源:WEB开发网最后,我们构造汉堡店,让这个故事发生:
public class HambergShop {
Waiter waiter = new Waiter();
HambergFifo hambergPool = new HambergFifo();
Customer c1 = new Customer(waiter, hambergPool);
Customer c2 = new Customer(waiter, hambergPool);
Customer c3 = new Customer(waiter, hambergPool);
Cooker cooker = new Cooker(waiter, hambergPool);
public static void main(String[] args) {
HambergShop hambergShop = new HambergShop();
Thread t1 = new Thread(hambergShop.c1, "Customer 1");
Thread t2 = new Thread(hambergShop.c2, "Customer 2");
Thread t3 = new Thread(hambergShop.c3, "Customer 3");
Thread t4 = new Thread(hambergShop.cooker, "Cooker 1");
Thread t5 = new Thread(hambergShop.cooker, "Cooker 2");
Thread t6 = new Thread(hambergShop.cooker, "Cooker 3");
t4.start();
t5.start();
t6.start();
try {
Thread.sleep(10000);
} catch (Exception e) {
}
t1.start();
t2.start();
t3.start();
}
}
更多精彩
赞助商链接