1 public class K { 2 //狀態鎖
3 private Object lock; 4 //條件變量
5 private int now,need; 6 public void produce(int num){ 7 //同步
8 synchronized (lock){ 9 //當前有的不知足須要,進行等待,直到知足條件
10 while(now < need){ 11 try { 12 //等待阻塞
13 lock.wait(); 14 } catch (InterruptedException e) { 15 e.printStackTrace(); 16 } 17 System.out.println("我被喚醒了!"); 18 } 19 // 作其餘的事情
20 } 21 } 22 } 23
1 import java.util.LinkedList; 2
3 /**
4 * 生產者和消費者的問題 5 * wait、notify/notifyAll() 實現 6 */
7 public class Storage1 implements AbstractStorage { 8 //倉庫最大容量
9 private final int MAX_SIZE = 100; 10 //倉庫存儲的載體
11 private LinkedList list = new LinkedList(); 12
13 //生產產品
14 public void produce(int num){ 15 //同步
16 synchronized (list){ 17 //倉庫剩餘的容量不足以存放即將要生產的數量,暫停生產
18 while(list.size()+num > MAX_SIZE){ 19 System.out.println("【要生產的產品數量】:" + num + "\t【庫存量】:"
20 + list.size() + "\t暫時不能執行生產任務!"); 21
22 try { 23 //條件不知足,生產阻塞
24 list.wait(); 25 } catch (InterruptedException e) { 26 e.printStackTrace(); 27 } 28 } 29
30 for(int i=0;i<num;i++){ 31 list.add(new Object()); 32 } 33
34 System.out.println("【已經生產產品數】:" + num + "\t【現倉儲量爲】:" + list.size()); 35
36 list.notifyAll(); 37 } 38 } 39
40 //消費產品
41 public void consume(int num){ 42 synchronized (list){ 43
44 //不知足消費條件
45 while(num > list.size()){ 46 System.out.println("【要消費的產品數量】:" + num + "\t【庫存量】:"
47 + list.size() + "\t暫時不能執行生產任務!"); 48
49 try { 50 list.wait(); 51 } catch (InterruptedException e) { 52 e.printStackTrace(); 53 } 54 } 55
56 //消費條件知足,開始消費
57 for(int i=0;i<num;i++){ 58 list.remove(); 59 } 60
61 System.out.println("【已經消費產品數】:" + num + "\t【現倉儲量爲】:" + list.size()); 62
63 list.notifyAll(); 64 } 65 } 66 }
- 抽象倉庫類java
1 public interface AbstractStorage { 2 void consume(int num); 3 void produce(int num); 4 }
- 生產者編程
1 public class Producer extends Thread{ 2 //每次生產的數量
3 private int num ; 4
5 //所屬的倉庫
6 public AbstractStorage abstractStorage; 7
8 public Producer(AbstractStorage abstractStorage){ 9 this.abstractStorage = abstractStorage; 10 } 11
12 public void setNum(int num){ 13 this.num = num; 14 } 15
16 // 線程run函數
17 @Override 18 public void run() 19 { 20 produce(num); 21 } 22
23 // 調用倉庫Storage的生產函數
24 public void produce(int num) 25 { 26 abstractStorage.produce(num); 27 } 28 }
- 消費者多線程
1 public class Consumer extends Thread{ 2 // 每次消費的產品數量
3 private int num; 4
5 // 所在放置的倉庫
6 private AbstractStorage abstractStorage1; 7
8 // 構造函數,設置倉庫
9 public Consumer(AbstractStorage abstractStorage1) 10 { 11 this.abstractStorage1 = abstractStorage1; 12 } 13
14 // 線程run函數
15 public void run() 16 { 17 consume(num); 18 } 19
20 // 調用倉庫Storage的生產函數
21 public void consume(int num) 22 { 23 abstractStorage1.consume(num); 24 } 25
26 public void setNum(int num){ 27 this.num = num; 28 } 29 }
- 測試ide
1 public class Test{ 2 public static void main(String[] args) { 3 // 倉庫對象
4 AbstractStorage abstractStorage = new Storage1(); 5
6 // 生產者對象
7 Producer p1 = new Producer(abstractStorage); 8 Producer p2 = new Producer(abstractStorage); 9 Producer p3 = new Producer(abstractStorage); 10 Producer p4 = new Producer(abstractStorage); 11 Producer p5 = new Producer(abstractStorage); 12 Producer p6 = new Producer(abstractStorage); 13 Producer p7 = new Producer(abstractStorage); 14
15 // 消費者對象
16 Consumer c1 = new Consumer(abstractStorage); 17 Consumer c2 = new Consumer(abstractStorage); 18 Consumer c3 = new Consumer(abstractStorage); 19
20 // 設置生產者產品生產數量
21 p1.setNum(10); 22 p2.setNum(10); 23 p3.setNum(10); 24 p4.setNum(10); 25 p5.setNum(10); 26 p6.setNum(10); 27 p7.setNum(80); 28
29 // 設置消費者產品消費數量
30 c1.setNum(50); 31 c2.setNum(20); 32 c3.setNum(30); 33
34 // 線程開始執行
35 c1.start(); 36 c2.start(); 37 c3.start(); 38
39 p1.start(); 40 p2.start(); 41 p3.start(); 42 p4.start(); 43 p5.start(); 44 p6.start(); 45 p7.start(); 46 } 47 }
- 輸出函數
【要消費的產品數量】:50 【庫存量】:0 暫時不能執行生產任務! 【要消費的產品數量】:20 【庫存量】:0 暫時不能執行生產任務! 【要消費的產品數量】:30 【庫存量】:0 暫時不能執行生產任務! 【已經生產產品數】:10 【現倉儲量爲】:10 【要消費的產品數量】:30 【庫存量】:10 暫時不能執行生產任務! 【要消費的產品數量】:20 【庫存量】:10 暫時不能執行生產任務! 【要消費的產品數量】:50 【庫存量】:10 暫時不能執行生產任務! 【已經生產產品數】:10 【現倉儲量爲】:20 【已經生產產品數】:10 【現倉儲量爲】:30 【要消費的產品數量】:50 【庫存量】:30 暫時不能執行生產任務! 【已經消費產品數】:20 【現倉儲量爲】:10 【要消費的產品數量】:30 【庫存量】:10 暫時不能執行生產任務! 【已經生產產品數】:10 【現倉儲量爲】:20 【要消費的產品數量】:50 【庫存量】:20 暫時不能執行生產任務! 【要消費的產品數量】:30 【庫存量】:20 暫時不能執行生產任務! 【已經生產產品數】:10 【現倉儲量爲】:30 【已經消費產品數】:30 【現倉儲量爲】:0 【要消費的產品數量】:50 【庫存量】:0 暫時不能執行生產任務! 【已經生產產品數】:10 【現倉儲量爲】:10 【要消費的產品數量】:50 【庫存量】:10 暫時不能執行生產任務! 【已經生產產品數】:80 【現倉儲量爲】:90 【已經消費產品數】:50 【現倉儲量爲】:40