代碼同「一輩子產與多消費」,區別在於測試類代碼:java
public class Test { public static void main(String[] args) { MyStack myStack = new MyStack(); Produce produce1 = new Produce(myStack); Produce produce2 = new Produce(myStack); Produce produce3 = new Produce(myStack); Produce produce4 = new Produce(myStack); Produce produce5 = new Produce(myStack); Consumer consumer = new Consumer(myStack); ThreadProduce threadProduce1 = new ThreadProduce(produce1); ThreadProduce threadProduce2 = new ThreadProduce(produce2); ThreadProduce threadProduce3 = new ThreadProduce(produce3); ThreadProduce threadProduce4 = new ThreadProduce(produce4); ThreadProduce threadProduce5 = new ThreadProduce(produce5); threadProduce1.start(); threadProduce2.start(); threadProduce3.start(); threadProduce4.start(); threadProduce5.start(); ThreadConsumer threadConsumer = new ThreadConsumer(consumer); threadConsumer.start(); } }