1. 程式人生 > >【設計模式】2.工廠設計模式(生產者消費者問題)

【設計模式】2.工廠設計模式(生產者消費者問題)

catch 設計 star 機制 print boolean 結果 try producer

生產者消費者模型 與 多線程

生產者、消費者在同一時間內共用同一存儲空間

生產者向共享空間生產數據,

而消費者取走共享的數據。、

經典問題描述:

生產者不斷交替地生產兩組數據“姓名--1 --> 內容--1”,“姓名--2--> 內容--2”,消費者不斷交替地取得這兩組數據。

多線程的情況下會出現以下不確定性的問題:

  • 生產者只生產了姓名還沒有生產內容信息, 程序就切換到消費者線程,結果是把姓名和上一次的內容聯系起來。
  • 生產者生產了若幹次數據,消費者才開始取數據,或者消費者取完一次數據後,還沒等生產者放入新的數據,又重復取出已取過的數據。

問題1 同步可以解決

問題2 線程通信可以解決

總結:生產者線程放入數據後,通知消費者線程取出數據,消費者線程取出數據後,通知生產者線程生產數據,這裏用 wait/notify 機制來實現。

【實例代碼】

 1 class Info {//定義信息類
 2     private String name = "name";
 3     private String content = "content";
 4     private boolean flag = true;// 設置標誌位,初始時先生產
 5     public synchronized void set(String name, String content) {
 6
while (!flag) {//wait狀態,等待其他線程,flag開始設置為TRUE,所以開始不執行 7 try { 8 super.wait(); 9 } catch (InterruptedException e) { 10 e.printStackTrace(); 11 } 12 } 13 this.setName(name); 14 try { 15 Thread.sleep(300);
16 } catch (InterruptedException e) { 17 e.printStackTrace(); 18 } 19 this.setContent(content); 20 flag = false;// 改變標誌位,表示可以取走 21 super.notify(); 22 } 23 public synchronized void get() { 24 while (flag) { 25 try { 26 super.wait(); 27 } catch (InterruptedException e) { 28 e.printStackTrace(); 29 } 30 } 31 try { 32 Thread.sleep(300); 33 } catch (InterruptedException e) { 34 e.printStackTrace(); 35 } 36 System.out.println(this.getName() + 37 "-->" + this.getContent()); 38 flag = true; 39 super.notify(); 40 } 41 public void setName(String name) { 42 this.name = name; 43 } 44 public void setContent(String content) { 45 this.content = content; 46 } 47 public String getName() { 48 return this.name; 49 } 50 public String getContent() { 51 return this.content; 52 } 53 } 54 class Producer implements Runnable { 55 private Info info = null; 56 public Producer(Info info) { 57 this.info = info; 58 } 59 public void run() { 60 boolean flag = true; 61 for (int i = 0; i < 10; i++) { 62 if (flag) { 63 this.info.set("姓名--1", "內容--1"); 64 flag = false; 65 }else { 66 this.info.set("姓名--2", "內容--2");; 67 flag = true; 68 } 69 } 70 } 71 } 72 class Consumer implements Runnable{ 73 private Info info = null ; 74 public Consumer(Info info){ 75 this.info = info ; 76 } 77 public void run(){ 78 for(int i=0;i<10;i++){ 79 this.info.get() ; 80 } 81 } 82 } 83 public class Main { 84 public static void main(String[] args) { 85 Info info = new Info(); 86 Producer pro = new Producer(info); 87 Consumer con = new Consumer(info) ; // 消費者 88 new Thread(pro).start() ; 89 //啟動了生產者線程後,再啟動消費者線程 90 try{ 91 Thread.sleep(500) ; 92 }catch(InterruptedException e){ 93 e.printStackTrace() ; 94 } 95 new Thread(con).start() ; 96 } 97 }

技術分享

【設計模式】2.工廠設計模式(生產者消費者問題)