Goods.java
package com.bebig.producerconsumer;

//商品
 public class Goods {
private int id;

 public Goods(int id) {
this.setId(id);
}

 public void setId(int id) {
this.id = id;
}

 public int getId() {
return id;
}
}

Cart.java
package com.bebig.producerconsumer;

//購物車
 public class Cart {
private final int MAXVALUE = 6;// 購物車里最多能存放的商品數量
private int index = 0;
private Goods[] goods = new Goods[MAXVALUE];

 public synchronized void push(String name, Goods g) {
 while (index == goods.length) {
// 購物車里滿了就不要放物品進來了
 try {
System.out.println("----購物車里的商品已滿,停止生產.");
this.wait();
 } catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
this.goods[index] = g;
index++;
System.out.println("生產者" + name + "向購物車里放置了一件商品.");

}

 public synchronized Goods pop(String name) {
 while (0 == index) {
// 購物車里空了就不要取物品出去了
 try {
System.out.println("----購物車里的商品已空,停止消費.");
this.wait();
 } catch (InterruptedException e) {
e.printStackTrace();
}
}
this.notifyAll();
index--;
System.out.println("消費者" + name + "從購物車取走了一件商品.");
return this.goods[index];
}

}
Producer.java
package com.bebig.producerconsumer;

//生產者
 public class Producer implements Runnable {
private Cart cart = null;
private String name;

 public Producer(Cart c, String name) {
this.cart = c;
this.setName(name);
}

@Override
 public void run() {
 for (int i = 1; i < 10; i++) {
cart.push(this.getName(), new Goods(i));
 try {
Thread.sleep((int) (Math.random() * 1000));
 } catch (InterruptedException e) {
e.printStackTrace();
}
}
}

 public void setName(String name) {
this.name = name;
}

 public String getName() {
return name;
}
}
Consumer.java
package com.bebig.producerconsumer;

//消費者
 public class Consumer implements Runnable {
private Cart cart = null;
private String name;

 public Consumer(Cart c, String name) {
this.name = name;
this.cart = c;
}

@Override
 public void run() {
 for (int i = 1; i < 10; i++) {
cart.pop(this.getName());
 try {
Thread.sleep((int) (Math.random() * 1000));
 } catch (InterruptedException e) {
e.printStackTrace();
}
}
}

 public void setName(String name) {
this.name = name;
}

 public String getName() {
return name;
}

}
測試代碼:
package com.bebig.producerconsumer;

//測試
 public class ProducerAndConsumer {

 /** *//**
* @param args
*/
 public static void main(String[] args) {
Cart c = new Cart();

Producer p1 = new Producer(c, "Don");
Consumer c1 = new Consumer(c, "Scan");
Producer p2 = new Producer(c, "txc");
Consumer c2 = new Consumer(c, "Sun");
new Thread(p1).start();
new Thread(c1).start();
new Thread(p2).start();
new Thread(c2).start();

}

}

測試結果(隨機顯示):
生產者Don向購物車里放置了一件商品.
生產者txc向購物車里放置了一件商品.
消費者Sun從購物車取走了一件商品.
消費者Scan從購物車取走了一件商品.
----購物車里的商品已空,停止消費.
生產者txc向購物車里放置了一件商品.
消費者Sun從購物車取走了一件商品.
----購物車里的商品已空,停止消費.
生產者txc向購物車里放置了一件商品.
消費者Scan從購物車取走了一件商品.
生產者Don向購物車里放置了一件商品.
生產者Don向購物車里放置了一件商品.
生產者txc向購物車里放置了一件商品.
消費者Sun從購物車取走了一件商品.
消費者Scan從購物車取走了一件商品.
生產者Don向購物車里放置了一件商品.
生產者txc向購物車里放置了一件商品.
消費者Sun從購物車取走了一件商品.
生產者txc向購物車里放置了一件商品.
消費者Scan從購物車取走了一件商品.
生產者txc向購物車里放置了一件商品.
生產者Don向購物車里放置了一件商品.
生產者txc向購物車里放置了一件商品.
消費者Scan從購物車取走了一件商品.
消費者Scan從購物車取走了一件商品.
消費者Sun從購物車取走了一件商品.
生產者Don向購物車里放置了一件商品.
生產者txc向購物車里放置了一件商品.
消費者Sun從購物車取走了一件商品.
消費者Scan從購物車取走了一件商品.
消費者Sun從購物車取走了一件商品.
消費者Scan從購物車取走了一件商品.
----購物車里的商品已空,停止消費.
生產者Don向購物車里放置了一件商品.
消費者Sun從購物車取走了一件商品.
----購物車里的商品已空,停止消費.
生產者Don向購物車里放置了一件商品.
消費者Scan從購物車取走了一件商品.
----購物車里的商品已空,停止消費.
生產者Don向購物車里放置了一件商品.
消費者Sun從購物車取走了一件商品.
|
|
CALENDER
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
---|
28 | 29 | 30 | 31 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
|
常用鏈接
留言簿
隨筆分類
隨筆檔案
文章分類
文章檔案
新聞檔案
相冊
搜索
最新評論

閱讀排行榜
評論排行榜
Powered By: 博客園 模板提供:滬江博客
|