1. 程式人生 > >生產者/消費者問題的多種Java實現方式

生產者/消費者問題的多種Java實現方式

放棄 所有 rod 暫時 其他 err 來看 empty cer

實質上,很多後臺服務程序並發控制的基本原理都可以歸納為生產者/消費者模式,

生產者消費者問題是研究多線程程序時繞不開的經典問題之一,它描述是有一塊緩沖區作為倉庫,生產者可以將產品放入倉庫,消費者則可以從倉庫中取走產品。解決生產者/消費者問題的方法可分為兩類:

(1)采用某種機制保護生產者和消費者之間的同步;

(2)在生產者和消費者之間建立一個管道。第一種方式有較高的效率,並且易於實現,代碼的可控制性較好,屬於常用的模式。第二種管道緩沖區不易控制,被傳輸數據對象不易於封裝等,實用性不強。因此本文只介紹同步機制實現的生產者/消費者問題。

同步問題核心在於:如何保證同一資源被多個線程並發訪問時的完整性。常用的同步方法是采用信號或加鎖機制,保證資源在任意時刻至多被一個線程訪問。

Java語言在多線程編程上實現了完全對象化,提供了對同步機制的良好支持。在Java中一共有四種方法支持同步,其中前三個是同步方法,一個是管道方法。

(1)wait() / notify()方法

(2)await() / signal()方法

(3)BlockingQueue阻塞隊列方法

(4)PipedInputStream / PipedOutputStream

本文只介紹最常用的前三種,第四種暫不做討論,有興趣的讀者可以自己去網上找答案。

一、wait() / notify()方法

wait() / nofity()方法是基類Object的兩個方法,也就意味著所有Java類都會擁有這兩個方法,這樣,我們就可以為任何對象實現同步機制。

wait()方法:當緩沖區已滿/空時,生產者/消費者線程停止自己的執行,放棄鎖,使自己處於等等狀態,讓其他線程執行。

notify()方法:當生產者/消費者向緩沖區放入/取出一個產品時,向其他等待的線程發出可執行的通知,同時放棄鎖,使自己處於等待狀態。

光看文字可能不太好理解,咱來段代碼就明白了:

  1. import java.util.LinkedList;
  2. /**
  3. * 倉庫類Storage實現緩沖區
  4. *
  5. * Email:[email protected]
  6. *
  7. * @author MONKEY.D.MENG 2011-03-15
  8. *
  9. */
  10. public class Storage
  11. {
  12. // 倉庫最大存儲量
  13. private final int MAX_SIZE = 100;
  14. // 倉庫存儲的載體
  15. private LinkedList<Object> list = new LinkedList<Object>();
  16. // 生產num個產品
  17. public void produce(int num)
  18. {
  19. // 同步代碼段
  20. synchronized (list)
  21. {
  22. // 如果倉庫剩余容量不足
  23. while (list.size() + num > MAX_SIZE)
  24. {
  25. System.out.println("【要生產的產品數量】:" + num + "/t【庫存量】:"
  26. + list.size() + "/t暫時不能執行生產任務!");
  27. try
  28. {
  29. // 由於條件不滿足,生產阻塞
  30. list.wait();
  31. }
  32. catch (InterruptedException e)
  33. {
  34. e.printStackTrace();
  35. }
  36. }
  37. // 生產條件滿足情況下,生產num個產品
  38. for (int i = 1; i <= num; ++i)
  39. {
  40. list.add(new Object());
  41. }
  42. System.out.println("【已經生產產品數】:" + num + "/t【現倉儲量為】:" + list.size());
  43. list.notifyAll();
  44. }
  45. }
  46. // 消費num個產品
  47. public void consume(int num)
  48. {
  49. // 同步代碼段
  50. synchronized (list)
  51. {
  52. // 如果倉庫存儲量不足
  53. while (list.size() < num)
  54. {
  55. System.out.println("【要消費的產品數量】:" + num + "/t【庫存量】:"
  56. + list.size() + "/t暫時不能執行生產任務!");
  57. try
  58. {
  59. // 由於條件不滿足,消費阻塞
  60. list.wait();
  61. }
  62. catch (InterruptedException e)
  63. {
  64. e.printStackTrace();
  65. }
  66. }
  67. // 消費條件滿足情況下,消費num個產品
  68. for (int i = 1; i <= num; ++i)
  69. {
  70. list.remove();
  71. }
  72. System.out.println("【已經消費產品數】:" + num + "/t【現倉儲量為】:" + list.size());
  73. list.notifyAll();
  74. }
  75. }
  76. // get/set方法
  77. public LinkedList<Object> getList()
  78. {
  79. return list;
  80. }
  81. public void setList(LinkedList<Object> list)
  82. {
  83. this.list = list;
  84. }
  85. public int getMAX_SIZE()
  86. {
  87. return MAX_SIZE;
  88. }
  89. }
  90. /**
  91. * 生產者類Producer繼承線程類Thread
  92. *
  93. * Email:[email protected]
  94. *
  95. * @author MONKEY.D.MENG 2011-03-15
  96. *
  97. */
  98. public class Producer extends Thread
  99. {
  100. // 每次生產的產品數量
  101. private int num;
  102. // 所在放置的倉庫
  103. private Storage storage;
  104. // 構造函數,設置倉庫
  105. public Producer(Storage storage)
  106. {
  107. this.storage = storage;
  108. }
  109. // 線程run函數
  110. public void run()
  111. {
  112. produce(num);
  113. }
  114. // 調用倉庫Storage的生產函數
  115. public void produce(int num)
  116. {
  117. storage.produce(num);
  118. }
  119. // get/set方法
  120. public int getNum()
  121. {
  122. return num;
  123. }
  124. public void setNum(int num)
  125. {
  126. this.num = num;
  127. }
  128. public Storage getStorage()
  129. {
  130. return storage;
  131. }
  132. public void setStorage(Storage storage)
  133. {
  134. this.storage = storage;
  135. }
  136. }
  137. /**
  138. * 消費者類Consumer繼承線程類Thread
  139. *
  140. * Email:[email protected]
  141. *
  142. * @author MONKEY.D.MENG 2011-03-15
  143. *
  144. */
  145. public class Consumer extends Thread
  146. {
  147. // 每次消費的產品數量
  148. private int num;
  149. // 所在放置的倉庫
  150. private Storage storage;
  151. // 構造函數,設置倉庫
  152. public Consumer(Storage storage)
  153. {
  154. this.storage = storage;
  155. }
  156. // 線程run函數
  157. public void run()
  158. {
  159. consume(num);
  160. }
  161. // 調用倉庫Storage的生產函數
  162. public void consume(int num)
  163. {
  164. storage.consume(num);
  165. }
  166. // get/set方法
  167. public int getNum()
  168. {
  169. return num;
  170. }
  171. public void setNum(int num)
  172. {
  173. this.num = num;
  174. }
  175. public Storage getStorage()
  176. {
  177. return storage;
  178. }
  179. public void setStorage(Storage storage)
  180. {
  181. this.storage = storage;
  182. }
  183. }
  184. /**
  185. * 測試類Test
  186. *
  187. * Email:[email protected]
  188. *
  189. * @author MONKEY.D.MENG 2011-03-15
  190. *
  191. */
  192. public class Test
  193. {
  194. public static void main(String[] args)
  195. {
  196. // 倉庫對象
  197. Storage storage = new Storage();
  198. // 生產者對象
  199. Producer p1 = new Producer(storage);
  200. Producer p2 = new Producer(storage);
  201. Producer p3 = new Producer(storage);
  202. Producer p4 = new Producer(storage);
  203. Producer p5 = new Producer(storage);
  204. Producer p6 = new Producer(storage);
  205. Producer p7 = new Producer(storage);
  206. // 消費者對象
  207. Consumer c1 = new Consumer(storage);
  208. Consumer c2 = new Consumer(storage);
  209. Consumer c3 = new Consumer(storage);
  210. // 設置生產者產品生產數量
  211. p1.setNum(10);
  212. p2.setNum(10);
  213. p3.setNum(10);
  214. p4.setNum(10);
  215. p5.setNum(10);
  216. p6.setNum(10);
  217. p7.setNum(80);
  218. // 設置消費者產品消費數量
  219. c1.setNum(50);
  220. c2.setNum(20);
  221. c3.setNum(30);
  222. // 線程開始執行
  223. c1.start();
  224. c2.start();
  225. c3.start();
  226. p1.start();
  227. p2.start();
  228. p3.start();
  229. p4.start();
  230. p5.start();
  231. p6.start();
  232. p7.start();
  233. }
  234. }
  235. 【要消費的產品數量】:50 【庫存量】:0 暫時不能執行生產任務!
  236. 【要消費的產品數量】:30 【庫存量】:0 暫時不能執行生產任務!
  237. 【要消費的產品數量】:20 【庫存量】:0 暫時不能執行生產任務!
  238. 【已經生產產品數】:10 【現倉儲量為】:10
  239. 【要消費的產品數量】:20 【庫存量】:10 暫時不能執行生產任務!
  240. 【要消費的產品數量】:30 【庫存量】:10 暫時不能執行生產任務!
  241. 【要消費的產品數量】:50 【庫存量】:10 暫時不能執行生產任務!
  242. 【已經生產產品數】:10 【現倉儲量為】:20
  243. 【要消費的產品數量】:50 【庫存量】:20 暫時不能執行生產任務!
  244. 【要消費的產品數量】:30 【庫存量】:20 暫時不能執行生產任務!
  245. 【已經消費產品數】:20 【現倉儲量為】:0
  246. 【已經生產產品數】:10 【現倉儲量為】:10
  247. 【已經生產產品數】:10 【現倉儲量為】:20
  248. 【已經生產產品數】:80 【現倉儲量為】:100
  249. 【要生產的產品數量】:10 【庫存量】:100 暫時不能執行生產任務!
  250. 【已經消費產品數】:30 【現倉儲量為】:70
  251. 【已經消費產品數】:50 【現倉儲量為】:20
  252. 【已經生產產品數】:10 【現倉儲量為】:30
  253. 【已經生產產品數】:10 【現倉儲量為】:40

看完上述代碼,對wait() / notify()方法實現的同步有了了解。你可能會對Storage類中為什麽要定義public void produce(int num);和public void consume(int num);方法感到不解,為什麽不直接在生產者類Producer和消費者類Consumer中實現這兩個方法,卻要調用Storage類中的實現呢?淡定,後文會有解釋。我們先往下走。

二、await() / signal()方法

在JDK5.0之後,Java提供了更加健壯的線程處理機制,包括同步、鎖定、線程池等,它們可以實現更細粒度的線程控制。await()和signal()就是其中用來做同步的兩種方法,它們的功能基本上和wait() / nofity()相同,完全可以取代它們,但是它們和新引入的鎖定機制Lock直接掛鉤,具有更大的靈活性。通過在Lock對象上調用newCondition()方法,將條件變量和一個鎖對象進行綁定,進而控制並發程序訪問競爭資源的安全。下面來看代碼:

  1. import java.util.LinkedList;
  2. import java.util.concurrent.locks.Condition;
  3. import java.util.concurrent.locks.Lock;
  4. import java.util.concurrent.locks.ReentrantLock;
  5. /**
  6. * 倉庫類Storage實現緩沖區
  7. *
  8. * Email:[email protected]
  9. *
  10. * @author MONKEY.D.MENG 2011-03-15
  11. *
  12. */
  13. public class Storage
  14. {
  15. // 倉庫最大存儲量
  16. private final int MAX_SIZE = 100;
  17. // 倉庫存儲的載體
  18. private LinkedList<Object> list = new LinkedList<Object>();
  19. // 鎖
  20. private final Lock lock = new ReentrantLock();
  21. // 倉庫滿的條件變量
  22. private final Condition full = lock.newCondition();
  23. // 倉庫空的條件變量
  24. private final Condition empty = lock.newCondition();
  25. // 生產num個產品
  26. public void produce(int num)
  27. {
  28. // 獲得鎖
  29. lock.lock();
  30. // 如果倉庫剩余容量不足
  31. while (list.size() + num > MAX_SIZE)
  32. {
  33. System.out.println("【要生產的產品數量】:" + num + "/t【庫存量】:" + list.size()
  34. + "/t暫時不能執行生產任務!");
  35. try
  36. {
  37. // 由於條件不滿足,生產阻塞
  38. full.await();
  39. }
  40. catch (InterruptedException e)
  41. {
  42. e.printStackTrace();
  43. }
  44. }
  45. // 生產條件滿足情況下,生產num個產品
  46. for (int i = 1; i <= num; ++i)
  47. {
  48. list.add(new Object());
  49. }
  50. System.out.println("【已經生產產品數】:" + num + "/t【現倉儲量為】:" + list.size());
  51. // 喚醒其他所有線程
  52. full.signalAll();
  53. empty.signalAll();
  54. // 釋放鎖
  55. lock.unlock();
  56. }
  57. // 消費num個產品
  58. public void consume(int num)
  59. {
  60. // 獲得鎖
  61. lock.lock();
  62. // 如果倉庫存儲量不足
  63. while (list.size() < num)
  64. {
  65. System.out.println("【要消費的產品數量】:" + num + "/t【庫存量】:" + list.size()
  66. + "/t暫時不能執行生產任務!");
  67. try
  68. {
  69. // 由於條件不滿足,消費阻塞
  70. empty.await();
  71. }
  72. catch (InterruptedException e)
  73. {
  74. e.printStackTrace();
  75. }
  76. }
  77. // 消費條件滿足情況下,消費num個產品
  78. for (int i = 1; i <= num; ++i)
  79. {
  80. list.remove();
  81. }
  82. System.out.println("【已經消費產品數】:" + num + "/t【現倉儲量為】:" + list.size());
  83. // 喚醒其他所有線程
  84. full.signalAll();
  85. empty.signalAll();
  86. // 釋放鎖
  87. lock.unlock();
  88. }
  89. // set/get方法
  90. public int getMAX_SIZE()
  91. {
  92. return MAX_SIZE;
  93. }
  94. public LinkedList<Object> getList()
  95. {
  96. return list;
  97. }
  98. public void setList(LinkedList<Object> list)
  99. {
  100. this.list = list;
  101. }
  102. }
  103. 【要消費的產品數量】:50 【庫存量】:0 暫時不能執行生產任務!
  104. 【要消費的產品數量】:30 【庫存量】:0 暫時不能執行生產任務!
  105. 【已經生產產品數】:10 【現倉儲量為】:10
  106. 【已經生產產品數】:10 【現倉儲量為】:20
  107. 【要消費的產品數量】:50 【庫存量】:20 暫時不能執行生產任務!
  108. 【要消費的產品數量】:30 【庫存量】:20 暫時不能執行生產任務!
  109. 【已經生產產品數】:10 【現倉儲量為】:30
  110. 【要消費的產品數量】:50 【庫存量】:30 暫時不能執行生產任務!
  111. 【已經消費產品數】:20 【現倉儲量為】:10
  112. 【已經生產產品數】:10 【現倉儲量為】:20
  113. 【要消費的產品數量】:30 【庫存量】:20 暫時不能執行生產任務!
  114. 【已經生產產品數】:80 【現倉儲量為】:100
  115. 【要生產的產品數量】:10 【庫存量】:100 暫時不能執行生產任務!
  116. 【已經消費產品數】:50 【現倉儲量為】:50
  117. 【已經生產產品數】:10 【現倉儲量為】:60
  118. 【已經消費產品數】:30 【現倉儲量為】:30
  119. 【已經生產產品數】:10 【現倉儲量為】:40

只需要更新倉庫類Storage的代碼即可,生產者Producer、消費者Consumer、測試類Test的代碼均不需要進行任何更改。這樣我們就知道為神馬我要在Storage類中定義public void produce(int num);和public void consume(int num);方法,並在生產者類Producer和消費者類Consumer中調用Storage類中的實現了吧。將可能發生的變化集中到一個類中,不影響原有的構架設計,同時無需修改其他業務層代碼。無意之中,我們好像使用了某種設計模式,具體是啥我忘記了,啊哈哈,等我想起來再告訴大家~

三、BlockingQueue阻塞隊列方法

BlockingQueue是JDK5.0的新增內容,它是一個已經在內部實現了同步的隊列,實現方式采用的是我們第2種await() / signal()方法。它可以在生成對象時指定容量大小。它用於阻塞操作的是put()和take()方法。

put()方法:類似於我們上面的生產者線程,容量達到最大時,自動阻塞。

take()方法:類似於我們上面的消費者線程,容量為0時,自動阻塞。

關於BlockingQueue的內容網上有很多,大家可以自己搜,我在這不多介紹。下面直接看代碼,跟以往一樣,我們只需要更改倉庫類Storage的代碼即可:

  1. import java.util.concurrent.LinkedBlockingQueue;
  2. /**
  3. * 倉庫類Storage實現緩沖區
  4. *
  5. * Email:[email protected]
  6. *
  7. * @author MONKEY.D.MENG 2011-03-15
  8. *
  9. */
  10. public class Storage
  11. {
  12. // 倉庫最大存儲量
  13. private final int MAX_SIZE = 100;
  14. // 倉庫存儲的載體
  15. private LinkedBlockingQueue<Object> list = new LinkedBlockingQueue<Object>(
  16. 100);
  17. // 生產num個產品
  18. public void produce(int num)
  19. {
  20. // 如果倉庫剩余容量為0
  21. if (list.size() == MAX_SIZE)
  22. {
  23. System.out.println("【庫存量】:" + MAX_SIZE + "/t暫時不能執行生產任務!");
  24. }
  25. // 生產條件滿足情況下,生產num個產品
  26. for (int i = 1; i <= num; ++i)
  27. {
  28. try
  29. {
  30. // 放入產品,自動阻塞
  31. list.put(new Object());
  32. }
  33. catch (InterruptedException e)
  34. {
  35. e.printStackTrace();
  36. }
  37. System.out.println("【現倉儲量為】:" + list.size());
  38. }
  39. }
  40. // 消費num個產品
  41. public void consume(int num)
  42. {
  43. // 如果倉庫存儲量不足
  44. if (list.size() == 0)
  45. {
  46. System.out.println("【庫存量】:0/t暫時不能執行生產任務!");
  47. }
  48. // 消費條件滿足情況下,消費num個產品
  49. for (int i = 1; i <= num; ++i)
  50. {
  51. try
  52. {
  53. // 消費產品,自動阻塞
  54. list.take();
  55. }
  56. catch (InterruptedException e)
  57. {
  58. e.printStackTrace();
  59. }
  60. }
  61. System.out.println("【現倉儲量為】:" + list.size());
  62. }
  63. // set/get方法
  64. public LinkedBlockingQueue<Object> getList()
  65. {
  66. return list;
  67. }
  68. public void setList(LinkedBlockingQueue<Object> list)
  69. {
  70. this.list = list;
  71. }
  72. public int getMAX_SIZE()
  73. {
  74. return MAX_SIZE;
  75. }
  76. }
  77. 【庫存量】:0 暫時不能執行生產任務!
  78. 【庫存量】:0 暫時不能執行生產任務!
  79. 【現倉儲量為】:1
  80. 【現倉儲量為】:1
  81. 【現倉儲量為】:3
  82. 【現倉儲量為】:4
  83. 【現倉儲量為】:5
  84. 【現倉儲量為】:6
  85. 【現倉儲量為】:7
  86. 【現倉儲量為】:8
  87. 【現倉儲量為】:9
  88. 【現倉儲量為】:10
  89. 【現倉儲量為】:11
  90. 【現倉儲量為】:1
  91. 【現倉儲量為】:2
  92. 【現倉儲量為】:13
  93. 【現倉儲量為】:14
  94. 【現倉儲量為】:17
  95. 【現倉儲量為】:19
  96. 【現倉儲量為】:20
  97. 【現倉儲量為】:21
  98. 【現倉儲量為】:22
  99. 【現倉儲量為】:23
  100. 【現倉儲量為】:24
  101. 【現倉儲量為】:25
  102. 【現倉儲量為】:26
  103. 【現倉儲量為】:12
  104. 【現倉儲量為】:1
  105. 【現倉儲量為】:1
  106. 【現倉儲量為】:2
  107. 【現倉儲量為】:3
  108. 【現倉儲量為】:4
  109. 【現倉儲量為】:5
  110. 【現倉儲量為】:6
  111. 【現倉儲量為】:7
  112. 【現倉儲量為】:27
  113. 【現倉儲量為】:8
  114. 【現倉儲量為】:6
  115. 【現倉儲量為】:18
  116. 【現倉儲量為】:2
  117. 【現倉儲量為】:3
  118. 【現倉儲量為】:4
  119. 【現倉儲量為】:5
  120. 【現倉儲量為】:6
  121. 【現倉儲量為】:7
  122. 【現倉儲量為】:8
  123. 【現倉儲量為】:9
  124. 【現倉儲量為】:10
  125. 【現倉儲量為】:16
  126. 【現倉儲量為】:11
  127. 【現倉儲量為】:12
  128. 【現倉儲量為】:13
  129. 【現倉儲量為】:14
  130. 【現倉儲量為】:15
  131. 【現倉儲量為】:1
  132. 【現倉儲量為】:2
  133. 【現倉儲量為】:3
  134. 【現倉儲量為】:3
  135. 【現倉儲量為】:15
  136. 【現倉儲量為】:1
  137. 【現倉儲量為】:0
  138. 【現倉儲量為】:1
  139. 【現倉儲量為】:1
  140. 【現倉儲量為】:1
  141. 【現倉儲量為】:2
  142. 【現倉儲量為】:3
  143. 【現倉儲量為】:4
  144. 【現倉儲量為】:0
  145. 【現倉儲量為】:1
  146. 【現倉儲量為】:5
  147. 【現倉儲量為】:6
  148. 【現倉儲量為】:7
  149. 【現倉儲量為】:8
  150. 【現倉儲量為】:9
  151. 【現倉儲量為】:10
  152. 【現倉儲量為】:11
  153. 【現倉儲量為】:12
  154. 【現倉儲量為】:13
  155. 【現倉儲量為】:14
  156. 【現倉儲量為】:15
  157. 【現倉儲量為】:16
  158. 【現倉儲量為】:17
  159. 【現倉儲量為】:1
  160. 【現倉儲量為】:1
  161. 【現倉儲量為】:2
  162. 【現倉儲量為】:3
  163. 【現倉儲量為】:4
  164. 【現倉儲量為】:5
  165. 【現倉儲量為】:6
  166. 【現倉儲量為】:3
  167. 【現倉儲量為】:3
  168. 【現倉儲量為】:1
  169. 【現倉儲量為】:2
  170. 【現倉儲量為】:3
  171. 【現倉儲量為】:4
  172. 【現倉儲量為】:5
  173. 【現倉儲量為】:6
  174. 【現倉儲量為】:7
  175. 【現倉儲量為】:8
  176. 【現倉儲量為】:9
  177. 【現倉儲量為】:10
  178. 【現倉儲量為】:11
  179. 【現倉儲量為】:12
  180. 【現倉儲量為】:13
  181. 【現倉儲量為】:14
  182. 【現倉儲量為】:15
  183. 【現倉儲量為】:16
  184. 【現倉儲量為】:17
  185. 【現倉儲量為】:18
  186. 【現倉儲量為】:19
  187. 【現倉儲量為】:6
  188. 【現倉儲量為】:7
  189. 【現倉儲量為】:8
  190. 【現倉儲量為】:9
  191. 【現倉儲量為】:10
  192. 【現倉儲量為】:11
  193. 【現倉儲量為】:12
  194. 【現倉儲量為】:13
  195. 【現倉儲量為】:14
  196. 【現倉儲量為】:15
  197. 【現倉儲量為】:16
  198. 【現倉儲量為】:17
  199. 【現倉儲量為】:18
  200. 【現倉儲量為】:19
  201. 【現倉儲量為】:20
  202. 【現倉儲量為】:21
  203. 【現倉儲量為】:22
  204. 【現倉儲量為】:23
  205. 【現倉儲量為】:24
  206. 【現倉儲量為】:25
  207. 【現倉儲量為】:26
  208. 【現倉儲量為】:27
  209. 【現倉儲量為】:28
  210. 【現倉儲量為】:29
  211. 【現倉儲量為】:30
  212. 【現倉儲量為】:31
  213. 【現倉儲量為】:32
  214. 【現倉儲量為】:33
  215. 【現倉儲量為】:34
  216. 【現倉儲量為】:35
  217. 【現倉儲量為】:36
  218. 【現倉儲量為】:37
  219. 【現倉儲量為】:38
  220. 【現倉儲量為】:39
  221. 【現倉儲量為】:40

當然,你會發現這時對於public void produce(int num);和public void consume(int num);方法業務邏輯上的實現跟前面兩個例子不太一樣,沒關系,這個例子只是為了說明BlockingQueue阻塞隊列的使用。

有時使用BlockingQueue可能會出現put()和System.out.println()輸出不匹配的情況,這是由於它們之間沒有同步造成的。當緩沖區已滿,生產者在put()操作時,put()內部調用了await()方法,放棄了線程的執行,然後消費者線程執行,調用take()方法,take()內部調用了signal()方法,通知生產者線程可以執行,致使在消費者的println()還沒運行的情況下生產者的println()先被執行,所以有了輸出不匹配的情況。

對於BlockingQueue大家可以放心使用,這可不是它的問題,只是在它和別的對象之間的同步有問題。

生產者/消費者問題的多種Java實現方式