多執行緒 生產者消費者模型

2021-09-27 07:28:51 字數 3437 閱讀 1097

1.簡介:

生產者消費者模式是通過乙個容器來解決生產者和消費者的強耦合問題。生產者和消費者彼此之間不直接通訊,而通過阻塞佇列來進行通訊,所以生產者生產完資料之後不用等待消費者處理,直接扔給阻塞佇列,消費者不找生產者要資料,而是直接從阻塞佇列裡取,阻塞佇列就相當於乙個緩衝區,平衡了生產者和消費者的處理能力。這個阻塞佇列就是用來給生產者和消費者解耦的

/**

* @author yan

* @create 2019/9/20 17:26 by intellij idea

* @description

*/class goods';

}//生產方法

public synchronized void set(string goodsname)

//消費方法

public synchronized void get()}/*

* 生產者

* */

class producer implements runnable

@override

public void run() }/*

* 消費者

* */

class consumer implements runnable

@override

public void run()

}//測試類

public class factorydesignmodel

}

執行結果:

goods

goods

但是將生產者執行緒開啟和消費者執行緒開啟的**換個位置在再測試下。此時問題產生了,生

產者還沒生產商品消費者就消費了導致數量不正確。此時就需要我們的wait()和notify()方法幫忙。

class goods

this.goodsname = goodsname;

this.count = count+1;

thread.sleep(1000);

system.out.println("生產:"+tostring());

//生產完商品通知消費者可一消費了

notify();

}//商品消費

public synchronized void get() throws interruptedexception

//每次消費乙個商品

this.count = this.count -1;

thread.sleep(1000);

system.out.println("消費"+tostring());

//消費完告訴生產者繼續生產

notify();

}@override

public string tostring() ';

}}/*

* 生產者

* */

class produser implements runnable

@override

public void run() catch (interruptedexception e)

}}/*

* 消費者

* */

class consumer implements runnable

@override

public void run() catch (interruptedexception e)

}}//測試類

public class factorydesignmodel

}

執行結果:

此時沒有商品,等待生產者生產

生產:goods

消費goods

多生產以及多消費

以上只有乙個生產者生產一次商品和乙個消費者只消費一次就結束了,現在能否改變一下,多個生產者和多個消費者呢?

分析一下,首先notify方法目前是只能喚醒乙個執行緒,如果有多個生產者執行緒和多個消費者執行緒的話,這個notify方法喚醒的執行緒如果是消費者的話應該沒有問題,但是如果是喚醒的也是生產者的執行緒那麼程式就會變成假死狀態了,這個時候顯然這個notify方法不行,我們上一節講過了有乙個notifyall()喚醒當前物件的所有執行緒。這個時候就可以使用該方法了,好了我們開始改造。

class goods

this.goodsname = goodsname;

this.count = count+1;

thread.sleep(1000);

system.out.println(thread.currentthread().getname());

system.out.println("生產"+tostring());

system.out.println("***********************************=");

//生產完商品通知消費者消費

notifyall();

}//商品消費

public synchronized void get() throws interruptedexception

//每次消費乙個商品

this.count = this.count -1;

thread.sleep(1000);

system.out.println(thread.currentthread().getname());

system.out.println("消費"+tostring());

system.out.println("****************************************");

//消費完告訴生產者繼續生產

notifyall();

}@override

public string tostring() ';

}}/*

* 生產者

* */

class produser implements runnable

@override

public void run() catch (interruptedexception e)

}}}/*

* 消費者

* */

class consumer implements runnable

@override

public void run() catch (interruptedexception e) }}

}//測試類

public class factorydesignmodel

//6個消費者執行緒

for (int i = 0; i < 6; i++)

for (thread thread:threadlist)

}}

多執行緒 生產者消費者

這個就不多說了,直接上 include include using namespace std const unsigned short size of buffer 10 緩衝區長度 unsigned short productid 0 產品號 unsigned short consumeid 0...

多執行緒之生產者消費者模型

生產者消費者 乙個最簡單的模型。兩個執行緒,乙個生產者,乙個消費者,生產者負責生產,消費者負責消費。分析 同步 生產者生產了之後,消費者進行讀取資料。wait 和notify機制 互斥 生產者生產時,消費者不能進行讀取。鎖機制。public class producerandconsumer cat...

執行緒,生產者消費者模型

什麼是執行緒 1.執行緒是cpu最小的執行單位 2.程序是資源單位 3.如果將作業系統比作工廠的話,程序是車間,執行緒是流水線,而cpu為電源 開啟乙個程序預設有乙個執行緒即主線程 執行緒的兩種開啟方式 例項化thread類 自定義mythread類,繼承thread類,覆蓋run方法 什麼時候需要...