Chinaunix首页 | 论坛 | 博客
  • 博客访问: 836652
  • 博文数量: 91
  • 博客积分: 2544
  • 博客等级: 少校
  • 技术积分: 1885
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-12 09:08
文章存档

2016年(10)

2014年(2)

2013年(4)

2012年(23)

2011年(23)

2010年(13)

2009年(14)

2007年(2)

分类: LINUX

2016-05-09 21:54:08

LinkedList实现的生产者消费者模型。
注意:对LinkedList的大小没有作限制,但在实际应用过程中该队列的大小应该是要做限制,以免耗尽内存。当对LinkedList的最大元素个数做限制后,实际上就成了一个又固定大小的循环队列。此时要对生产者的行为做一个约定,是覆盖老的数据,还是等待消费者消费掉数据后,又空位置再继续生产。

这里的程序没有对LinkedList大小做限制,代码如下:


点击(此处)折叠或打开

  1. package ProCon;

  2. import java.util.LinkedList;
  3. import java.util.Random;
  4. import java.util.concurrent.SynchronousQueue;

  5. import javax.naming.InterruptedNamingException;

  6. class Worker {
  7.     LinkedList<String> JobQueue = new LinkedList<String>();
  8.     
  9.     Worker() {}
  10.     
  11.     // consumer
  12.     public class Consumer implements Runnable {
  13.         String item = null;
  14.         
  15.         public void run() {
  16.             for ( ; ; ) {
  17.                 synchronized (JobQueue) {
  18.                     System.out.println("cons get lock");
  19.                     while (JobQueue.size() == 0) {
  20.                         System.out.println("cons wait . release lock");
  21.                             try {
  22.                                 JobQueue.wait();
  23.                             } catch (InterruptedException e) {
  24.                                 // TODO Auto-generated catch block
  25.                                 e.printStackTrace();
  26.                             }
  27.                     }
  28.                     item = JobQueue.remove();
  29.                     System.out.println("consumer get: " + item);
  30.                 }
  31.                 
  32.                 // wait for a while
  33.                 try {
  34.                     Thread.sleep(1000);
  35.                 } catch (InterruptedException e) {
  36.                     // TODO Auto-generated catch block
  37.                     e.printStackTrace();
  38.                 }
  39.             }
  40.             
  41.         }
  42.     }
  43.     
  44.     // producer
  45.     public class Producer implements Runnable {
  46.         Random r;
  47.         StringBuffer sb = new StringBuffer();
  48.         
  49.         Producer() {
  50.             r = new Random();
  51.         }
  52.         public void run() {
  53.             for ( ; ; ) {
  54.                 System.out.println("producer loop ");
  55.                 synchronized (JobQueue) {
  56.                     System.out.println("producer get lock");
  57.                     sb.delete(0, sb.length());
  58.                     sb.append("").append(r.nextInt(100));
  59.                     System.out.println("put : " + sb.toString());

  60.                     JobQueue.add(sb.toString());
  61.                     JobQueue.notifyAll();
  62.                 }
  63.                 
  64.                 System.out.println("producer release lock");
  65.                 // wait for a while
  66.                 try {
  67.                     Thread.sleep(1000);
  68.                 } catch (InterruptedException e) {
  69.                     // TODO Auto-generated catch block
  70.                     e.printStackTrace();
  71.                 }
  72.             }
  73.         }
  74.     }
  75.     
  76.     
  77.     // work produce
  78.     public void run() {
  79.         Producer p = new Producer();
  80.         Consumer c = new Consumer();
  81.         
  82.         new Thread(p).start();
  83.         new Thread(p).start();
  84.         
  85.         // consumer
  86.         new Thread(c).start();
  87.         new Thread(c).start();
  88.         new Thread(c).start();
  89.     }
  90. }

  91. public class TestProCon {
  92.     public static void main(String[] args) throws InterruptedException {
  93.         Worker w = new Worker();
  94.         
  95.         w.run();
  96.         System.out.println("end main");
  97.         Thread.currentThread().join();
  98.     }
  99. }
运行效果如下:

点击(此处)折叠或打开

  1. producer loop
  2. producer get lock
  3. Thread-0 put : 56
  4. producer release lock
  5. producer loop
  6. producer get lock
  7. Thread-1 put : 36
  8. producer release lock
  9. cons get lock
  10. end main
  11. Thread-2 consumer get: 56
  12. cons get lock
  13. Thread-3 consumer get: 36
  14. cons get lock
  15. cons wait . release lock
  16. producer loop
  17. producer get lock
  18. Thread-0 put : 98
  19. Thread-4 consumer get: 98
  20. producer release lock
  21. producer loop
  22. producer get lock
  23. Thread-1 put : 36
  24. producer release lock
  25. cons get lock
  26. Thread-2 consumer get: 36
  27. cons get lock
  28. cons wait . release lock
  29. cons get lock
  30. cons wait . release lock
  31. producer loop
  32. producer get lock
  33. Thread-0 put : 15
  34. producer release lock
  35. Thread-4 consumer get: 15
  36. cons wait . release lock
  37. producer loop
  38. cons get lock
  39. cons wait . release lock
  40. producer get lock
  41. Thread-1 put : 30
  42. producer release lock
  43. Thread-2 consumer get: 30
  44. cons wait . release lock
  45. producer loop
  46. cons get lock
  47. cons wait . release lock
  48. producer get lock
  49. Thread-0 put : 79
  50. producer release lock
  51. Thread-4 consumer get: 79
  52. cons wait . release lock
  53. producer loop
  54. cons get lock
  55. cons wait . release lock
  56. producer get lock
  57. Thread-1 put : 13
  58. producer release lock
  59. Thread-2 consumer get: 13
  60. cons wait . release lock
  61. producer loop
  62. cons get lock
  63. cons wait . release lock
  64. producer get lock
  65. Thread-0 put : 70
  66. Thread-4 consumer get: 70
  67. producer release lock
  68. cons wait . release lock
  69. cons get lock
  70. cons wait . release lock
  71. producer loop
  72. producer get lock
  73. Thread-1 put : 15
  74. producer release lock
  75. Thread-2 consumer get: 15
  76. cons wait . release lock
从结果中可以看到是多个生产者,和多个消费者同时在运行。


阅读(2627) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~