67,550
社区成员




class Order {
private static int i = 0;
private int count = i++;
private WaitPerson waiter;
public Order(WaitPerson w) {
this.waiter = w;
if(count == 10){
System.out.println("Out of food, closing");
System.exit(0);
}
}
public WaitPerson getWaiter(){
return this.waiter;
}
public String toString() { return "Order" + count; }
}
class Custom extends Thread {
int num;
Restaurant restaurant;
WaitPerson waitPerson;
Custom(int num, Restaurant r, WaitPerson w){
this.num = num;
this.restaurant = r;
this.waitPerson = w;
start();
}
public void run(){
try {
while(true){
sleep(1000);
synchronized (restaurant.getOrderList()) {
Order order = new Order(this.waitPerson);
restaurant.newOrder(order);
restaurant.getOrderList().notifyAll();
System.out.println("No." + this.num + " custom order up " + order);
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String toString() {return "";}
}
class WaitPerson extends Thread {
private Restaurant restaurant;
private String name;
public WaitPerson(String name, Restaurant r){
restaurant = r;
this.name = name;
start();
}
public void run(){
while(true){
try {
synchronized (restaurant.getReadyList()) {
while(restaurant.getReady(this) == null){
synchronized (this) {
this.wait();
}
}
Order removed = restaurant.removeReady(this);
System.out.println(this.name + " got " + removed);
}
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}
class Chef extends Thread{
private String name;
private Restaurant restaurant;
public Chef(String name,Restaurant r){
this.name = name;
restaurant = r;
start();
}
public void run(){
while(true){
synchronized (restaurant.getOrderList()) {
while(restaurant.getFirstOrder() == null){
try {
restaurant.getOrderList().wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
try {
sleep(400);
} catch (InterruptedException e) {
e.printStackTrace();
}//模拟做菜4秒
Order o = restaurant.getFirstOrder();
restaurant.newReady(o.getWaiter(), o);
System.out.println(this.name + " done order " + o);
synchronized (o.getWaiter()) {
o.getWaiter().notify();
}
}
}
}
}
public class Restaurant {
private List<Order> orderList = new ArrayList<Order>();
private Map<WaitPerson,List<Order>> readyMap = new HashMap<WaitPerson,List<Order>>();
public List<Order> getOrderList(){
return orderList;
}
public Map<WaitPerson,List<Order>> getReadyList(){
return readyMap;
}
public void newOrder(Order o){
orderList.add(o);
}
public Order getFirstOrder(){
return (orderList.size() == 0) ? null : orderList.get(0);
}
public Order newReady(WaitPerson w,Order o){
if(readyMap.get(w) != null){
readyMap.get(w).add(o);
}else{
List<Order> readyOrderlist = new ArrayList<Order>();
readyOrderlist.add(o);
readyMap.put(w, readyOrderlist);
}
orderList.remove(0);
return o;
}
public Order getReady(WaitPerson w){
List<Order> olist = new ArrayList<Order>();
olist = readyMap.get(w);
if(olist != null){
return (olist.size() == 0) ? null : olist.get(0);
}else{
return null;
}
}
public Order removeReady(WaitPerson w){
return readyMap.get(w).remove(0);
}
public static void main(String[] args){
Restaurant restaurant = new Restaurant();
WaitPerson waitPerson1 = new WaitPerson("aaa", restaurant);
WaitPerson waitPerson2 = new WaitPerson("bbb", restaurant);
WaitPerson waitPerson3 = new WaitPerson("ccc", restaurant);
WaitPerson waitPerson4 = new WaitPerson("ddd", restaurant);
Custom c1 = new Custom(1,restaurant,waitPerson4);
Custom c2 = new Custom(2,restaurant,waitPerson3);
Custom c3 = new Custom(3,restaurant,waitPerson2);
Custom c4 = new Custom(4,restaurant,waitPerson1);
Custom c5 = new Custom(5,restaurant,waitPerson4);
Custom c6 = new Custom(6,restaurant,waitPerson3);
Chef chef1 = new Chef("Jimmy",restaurant);
Chef chef2 = new Chef("Manny",restaurant);
}
}