/** * 要生產的產品 * @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(); } } // } } }
執行輸出:java
倉庫空啦!!!!
倉庫空啦!!!!
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個產品dom