/**
* 要生产的产品
* @author jimmywu
*
*/
public class Product {
private int id;
private String name;
public Product(int id,String name){
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "Product id:"+id+" name:"+name;
}
}
/**
* 仓库类
*
* @author jimmywu
*
*/
public class Store {
Product[] pArray = new Product[10];
AtomicInteger index = new AtomicInteger(0);
/**
* 生产方法
*
* @param p
*/
public synchronized void push(Product p) {
try {
while (index.get() == pArray.length) {
System.out.println("仓库已满!!!!!");
this.wait();
}
this.notify();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
pArray[index.getAndIncrement()] = p;
System.out.println(Thread.currentThread().getName() + " 当前生产:"
+ p.toString() + " 共" + index.get() + "个产品");
}
/**
* 消费方法
*
* @return
*/
public synchronized Product pop() {
try {
while (index.get() == 0) {
System.out.println("仓库空啦!!!!");
this.wait();
}
this.notify();
} catch (InterruptedException e) {
e.printStackTrace();
}
Product p = pArray[index.getAndDecrement() - 1];
System.out.println(Thread.currentThread().getName() + " 消费了"
+ p.toString() + "当前还有" + index.get() + "个");
return p;
}
public static void main(String[] args) {
Store s = new Store();
Thread p1 = new Thread(new Producer(s));
Thread p2 = new Thread(new Producer(s));
Thread c1 = new Thread(new Consumer(s));
Thread c2 = new Thread(new Consumer(s));
p1.start();
// p2.start();
c1.start();
c2.start();
}
}
class Producer implements Runnable {
Store store;
Producer(Store store) {
this.store = store;
}
/**
* 生产线程.
*/
public void run() {
// for (int i = 0; i < 20; i++) {
while(true){
Product p = new Product(new Random().nextInt(), String.valueOf(System
.currentTimeMillis()));
store.push(p);
try {
Thread.sleep((int) (Math.random() * 500));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// }
}
}
class Consumer implements Runnable {
Store store;
Consumer(Store store) {
this.store = store;
}
/**
* 消费线程.
*/
public void run() {
// for (int i = 0; i < 20; i++) {
while(true){
Product p = store.pop();
try {
Thread.sleep((int) (Math.random() * 1000));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// }
}
}
执行输出:
仓库空啦!!!!
仓库空啦!!!!
Thread-0 当前生产:Product id:-1301795692 name:1483024321535 共1个产品
Thread-2 消费了Product id:-1301795692 name:1483024321535当前还有0个
仓库空啦!!!!
Thread-0 当前生产:Product id:-527262198 name:1483024321695 共1个产品
Thread-3 消费了Product id:-527262198 name:1483024321695当前还有0个
Thread-0 当前生产:Product id:-562347048 name:1483024321848 共1个产品
Thread-0 当前生产:Product id:1334829650 name:1483024322076 共2个产品
Thread-3 消费了Product id:1334829650 name:1483024322076当前还有1个
Thread-0 当前生产:Product id:503010454 name:1483024322213 共2个产品
Thread-2 消费了Product id:503010454 name:1483024322213当前还有1个
Thread-2 消费了Product id:-562347048 name:1483024321848当前还有0个
仓库空啦!!!!
Thread-0 当前生产:Product id:1339084264 name:1483024322545 共1个产品
Thread-3 消费了Product id:1339084264 name:1483024322545当前还有0个
Thread-0 当前生产:Product id:-704459171 name:1483024322562 共1个产品
Thread-2 消费了Product id:-704459171 name:1483024322562当前还有0个
Thread-0 当前生产:Product id:1375661799 name:1483024323006 共1个产品
Thread-0 当前生产:Product id:-584270083 name:1483024323087 共2个产品
Thread-3 消费了Product id:-584270083 name:1483024323087当前还有1个
Thread-3 消费了Product id:1375661799 name:1483024323006当前还有0个
仓库空啦!!!!
Thread-0 当前生产:Product id:777716577 name:1483024323489 共1个产品