生產(chǎn)者消費者模式的幾種實現(xiàn)方式
拿我們生活中的例子來說,工廠生產(chǎn)出來的產(chǎn)品總是要輸出到外面使用的,這就是生產(chǎn)與消費的概念。
在我們實際的軟件開發(fā)過程中,經(jīng)常會碰到如下場景:某個模塊負責產(chǎn)生數(shù)據(jù),這些數(shù)據(jù)由另一個模塊來負責處理(此處的模塊是廣義的,可以是類、函數(shù)、線程、進程等)。
產(chǎn)生數(shù)據(jù)的模塊,就形象地稱為生產(chǎn)者;而處理數(shù)據(jù)的模塊,就稱為消費者。
第一種:采用wait—notify實現(xiàn)生產(chǎn)者消費者模式
1. 一生產(chǎn)者與一消費者:
2. 一生產(chǎn)者與多消費者:
第二種: 采用阻塞隊列實現(xiàn)生產(chǎn)者消費者模式
3. 使用阻塞隊列實現(xiàn)生產(chǎn)者消費者模式
相信大家都有去吃過日本料理。有個很誘人的餐食就是烤肉,烤肉師父會站在一邊一直烤肉,再將烤好的肉放在一個盤子中;而流著口水的我們這些食客會坐在一邊,只要盤子里有肉我們就會一直去吃。
在這個生活案例中,烤肉師父就是生產(chǎn)者,他就負責烤肉,烤完了就把肉放在盤子里,而不是直接遞給食客(即不用通知食客去吃肉),如果盤子肉滿,師父就會停一會,直到有人去食用烤肉后再去進行生產(chǎn)肉;而食客的我們就只是盯著盤子,一旦盤子有肉我們就負責去吃就行;
整個過程中食客與烤肉師父都不是直接打交道的,而是都與盤子進行交互。
盤子充當了一個緩沖區(qū)的概念,有東西生產(chǎn)出來就把東西放進去,盤子也是有大小限制,超過盤子大小就會阻塞生產(chǎn)者生產(chǎn),等待消費者去消費;當盤子為空的時候 ,即阻塞消費者消費,等待生產(chǎn)者去生產(chǎn)。
編程中阻塞隊列即可以實現(xiàn)盤子這個功能。
阻塞隊列的特點:
當隊列元素已滿的時候,阻塞插入操作;
當隊列元素為空的時候,阻塞獲取操作。
arrayblockingqueue 與 linkedblockingqueue都是支持fifo(先進先出),但是linkedblockingqueue是無界的,而arrayblockingqueue 是有界的。
下面使用阻塞隊列實現(xiàn)生產(chǎn)者消費者:
生產(chǎn)者:
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; //設(shè)置隊列緩存的大小。生產(chǎn)過程中超過這個大小就暫時停止生產(chǎn) 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( "正在生產(chǎn):" + task); //將生產(chǎn)出來的產(chǎn)品放在隊列緩存中 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() { //只要阻塞隊列中有任務(wù),就一直去消費 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; /** * 生產(chǎn)者消費者模式 * 使用阻塞隊列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是無界隊列,所以生產(chǎn)者會不斷去生產(chǎn),將生產(chǎn)出的任務(wù)放在隊列中,消費者去隊列中去消費:
如果改用有界阻塞隊列arrayblockingqueue,就可以初始化隊列的大小。則隊列中元素超過隊列大小的時候,生產(chǎn)者就會等待消費者消費一個再去生產(chǎn)一個:
測試代碼:
初始化一個大小為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(); } |
測試中,讓生產(chǎn)者生產(chǎn)速度略快,而消費者速度略慢一點。可以看到生產(chǎn)出來的產(chǎn)品序號與消費的產(chǎn)品序號差始終為10(隊列的大小):
總結(jié)
以上就是本文關(guān)于生產(chǎn)消費者模式實現(xiàn)方式和線程安全問題代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
原文鏈接:http://blog.csdn.net/noaman_wgs/article/details/66969905