生產者消費者模式的幾種實現方式
拿我們生活中的例子來說,工廠生產出來的產品總是要輸出到外面使用的,這就是生產與消費的概念。
在我們實際的軟件開發過程中,經常會碰到如下場景:某個模塊負責產生數據,這些數據由另一個模塊來負責處理(此處的模塊是廣義的,可以是類、函數、線程、進程等)。
產生數據的模塊,就形象地稱為生產者;而處理數據的模塊,就稱為消費者。
第一種:采用wait—notify實現生產者消費者模式
1. 一生產者與一消費者:
2. 一生產者與多消費者:
第二種: 采用阻塞隊列實現生產者消費者模式
3. 使用阻塞隊列實現生產者消費者模式
相信大家都有去吃過日本料理。有個很誘人的餐食就是烤肉,烤肉師父會站在一邊一直烤肉,再將烤好的肉放在一個盤子中;而流著口水的我們這些食客會坐在一邊,只要盤子里有肉我們就會一直去吃。
在這個生活案例中,烤肉師父就是生產者,他就負責烤肉,烤完了就把肉放在盤子里,而不是直接遞給食客(即不用通知食客去吃肉),如果盤子肉滿,師父就會停一會,直到有人去食用烤肉后再去進行生產肉;而食客的我們就只是盯著盤子,一旦盤子有肉我們就負責去吃就行;
整個過程中食客與烤肉師父都不是直接打交道的,而是都與盤子進行交互。
盤子充當了一個緩沖區的概念,有東西生產出來就把東西放進去,盤子也是有大小限制,超過盤子大小就會阻塞生產者生產,等待消費者去消費;當盤子為空的時候 ,即阻塞消費者消費,等待生產者去生產。
編程中阻塞隊列即可以實現盤子這個功能。
阻塞隊列的特點:
當隊列元素已滿的時候,阻塞插入操作;
當隊列元素為空的時候,阻塞獲取操作。
arrayblockingqueue 與 linkedblockingqueue都是支持fifo(先進先出),但是linkedblockingqueue是無界的,而arrayblockingqueue 是有界的。
下面使用阻塞隊列實現生產者消費者:
生產者:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
import java.util.concurrent.blockingqueue; public class producer implements runnable{ private final blockingqueue blockingqueue; //設置隊列緩存的大小。生產過程中超過這個大小就暫時停止生產 private final int queue_size = 10 ; public producer(blockingqueue blockingqueue){ this .blockingqueue = blockingqueue; } int task = 1 ; @override public void run() { while ( true ){ try { system.out.println( "正在生產:" + task); //將生產出來的產品放在隊列緩存中 blockingqueue.put(task); ++task; //讓其停止一會,便于查看效果 thread.sleep( 1000 ); } catch (interruptedexception e) { e.printstacktrace(); } } } } |
消費者:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import java.util.concurrent.blockingqueue; //消費者 public class consumer implements runnable{ private final blockingqueue blockingqueue; public consumer(blockingqueue blockingqueue){ this .blockingqueue = blockingqueue; } @override public void run() { //只要阻塞隊列中有任務,就一直去消費 while ( true ){ try { system.out.println( "正在消費: " + blockingqueue.take()); //讓其停止一會,便于查看效果 thread.sleep( 2000 ); } catch (interruptedexception e) { e.printstacktrace(); } } } } |
測試:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
import java.util.concurrent.blockingqueue; import java.util.concurrent.linkedblockingqueue; /** * 生產者消費者模式 * 使用阻塞隊列blockingqueue * @author wanggenshen * */ public class testconpro { public static void main(string[] args){ blockingqueue blockingqueue = new linkedblockingqueue( 5 ); producer p = new producer(blockingqueue); consumer c = new consumer(blockingqueue); thread tp = new thread(p); thread tc= new thread(c); tp.start(); tc.start(); } } |
因為linkedblockingqueue是無界隊列,所以生產者會不斷去生產,將生產出的任務放在隊列中,消費者去隊列中去消費:
如果改用有界阻塞隊列arrayblockingqueue,就可以初始化隊列的大小。則隊列中元素超過隊列大小的時候,生產者就會等待消費者消費一個再去生產一個:
測試代碼:
初始化一個大小為10的arrayblockingqueue:
1
2
3
4
5
6
7
8
9
|
public static void main(string[] args){ blockingqueue blockingqueue = new arrayblockingqueue( 10 ); producer p = new producer(blockingqueue); consumer c = new consumer(blockingqueue); thread tp = new thread(p); thread tc= new thread(c); tp.start(); tc.start(); } |
測試中,讓生產者生產速度略快,而消費者速度略慢一點。可以看到生產出來的產品序號與消費的產品序號差始終為10(隊列的大小):
總結
以上就是本文關于生產消費者模式實現方式和線程安全問題代碼示例的全部內容,希望對大家有所幫助。感興趣的朋友可以繼續參閱本站其他相關專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/noaman_wgs/article/details/66969905