Java multi-thread consumer producer model

Source: Internet
Author: User

  1. /* @author Shijin
  2. * Producer and Consumer models, the following points must be ensured:
  3. * 1 at the same time only one producer production method plus lock sychronized
  4. * 2 at the same time can only have a consumer consumer spending method plus lock sychronized
  5. * 3 producer production at the same time consumers can not consume production method lock sychronized
  6. * 4 consumer consumption at the same time producers can not produce consumption method lock sychronized
  7. * 5 Space when the consumer cannot continue to consume the consumption before the cycle to determine whether it is empty, empty the thread wait, release the lock allows other synchronization methods to execute
  8. * 6 When the space is full, the producer cannot continue to produce production before the cycle determines whether it is full or full, wait for the thread, release the lock to allow other synchronization methods to execute
  9. */
  10. Main class
  11. Class Producerconsumer
  12. {
  13. public static void Main (string[] args)
  14. {
  15. Stackbasket s = new Stackbasket ();
  16. Producer p = new Producer (s);
  17. Consumer C = new Consumer (s);
  18. Thread TP = new Thread (p);
  19. Thread TC = new Thread (c);
  20. Tp.start ();
  21. Tc.start ();
  22. }
  23. }
  24. //
  25. Class Mantou
  26. {
  27. private int id;
  28. Mantou (int id) {
  29. this.id = ID;
  30. }
  31. Public String toString () {
  32. return "Mantou:" + ID;
  33. }
  34. }
  35. Shared stack Space
  36. Class Stackbasket
  37. {
  38. Mantou sm[] = new mantou[6];
  39. int index = 0;
  40. /**  
  41. * Show Production method.
  42. * Show this method is synchronous method, hold method lock;
  43. * Show first cycle judgment full No, full words make the thread wait, release the Sync method lock, allow consumption;
  44. * Show first wakes the waiting consumption method when dissatisfied, but also can only let it enter the ready state,
  45. * Show etc production end release Sync method lock consumption to hold the lock for consumption
  46. * @param m element
  47. * @return No return value
  48. */
  49. public synchronized void push (Mantou m) {
  50. try{
  51. While (index = = sm.length) {
  52. System.out.println ("!!!!!!!!! Production is full of!!!!!!!!! ");
  53. this.wait ();
  54. }
  55. this.notify ();
  56. }catch (Interruptedexception e) {
  57. E.printstacktrace ();
  58. }catch (Illegalmonitorstateexception e) {
  59. E.printstacktrace ();
  60. }
  61. Sm[index] = m;
  62. index++;
  63. System.out.println ("produced:" + M + "Total" + index + "a steamed bun");
  64. }
  65. /**  
  66. * Show Consumption method
  67. * Show this method is synchronous method, hold method lock
  68. * Show first loop to determine whether null, empty words make the thread wait, release the synchronous method lock, allow production;
  69. * Show first wakes the waiting production method when it is not empty, but only allows it to enter the ready state
  70. * Show and other consumption end release Sync method lock production to hold the lock for production
  71. * @param b True indicates display, false means hidden
  72. * @return No return value
  73. */
  74. public synchronized Mantou pop () {
  75. try{
  76. While (index = = 0) {
  77. System.out.println ("!!!!!!!!! Consumption of Light!!!!!!!!! ");
  78. this.wait ();
  79. }
  80. this.notify ();
  81. }catch (Interruptedexception e) {
  82. E.printstacktrace ();
  83. }catch (Illegalmonitorstateexception e) {
  84. E.printstacktrace ();
  85. }
  86. index--;
  87. System.out.println ("consumption:---------" + sm[index] + "Total" + index + "a steamed bun");
  88. return Sm[index];
  89. }
  90. }
  91. Class Producer implements Runnable
  92. {
  93. Stackbasket ss = new Stackbasket ();
  94. Producer (Stackbasket ss) {
  95. THIS.SS = SS;
  96. }
  97. /**  
  98. * Show production process.
  99. */
  100. public Void Run () {
  101. For (int i = 0;i < 20;i++) {
  102. Mantou m = new Mantou (i);
  103. Ss.push (m);
  104. System.out.println ("produced:" + M + "Total" + Ss.index + "a steamed bun");
  105. It is inappropriate to test on the above line, and access to index should be in atomic operation, because it may be consumed before the output of the push, resulting in output confusion
  106. try{
  107. Thread.Sleep ((int) (Math.random () *500));
  108. }catch (Interruptedexception e) {
  109. E.printstacktrace ();
  110. }
  111. }
  112. }
  113. }
  114. Class Consumer implements Runnable
  115. {
  116. Stackbasket ss = new Stackbasket ();
  117. Consumer (Stackbasket ss) {
  118. THIS.SS = SS;
  119. }
  120. /**  
  121. * Show consumption process.
  122. */
  123. public Void Run () {
  124. For (int i = 0;i < 20;i++) {
  125. Mantou m = Ss.pop ();
  126. System.out.println ("Consumption:---------" + M + "Total" + Ss.index + "a steamed bun");
  127. It is not appropriate to test the above line in the above row, the access to index should be in the atomic operation, because it may be produced after the pop after this output, will produce output confusion
  128. try{
  129. Thread.Sleep ((int) (Math.random () *1000));
  130. }catch (Interruptedexception e) {
  131. E.printstacktrace ();
  132. }
  133. }
  134. }
  135. }

Java multi-thread consumer producer model

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.