Java Multithreading Summary Six: the classic producer consumer problem realization

Source: Internet
Author: User

This is a classic example of thread synchronization, the source code is as follows:

[Java]View Plaincopy
  1. <span style="FONT-SIZE:16PX;"  > Packagedemo.thread;
  2. /**
  3. * Classic producer and consumer issues: producers constantly store their products in warehouses and consumers consume products from warehouses.
  4. * Both producers and consumers can have a number of. Storage capacity is limited, storage is not available when the library is full, no products can be taken when the library is empty
  5. */
  6. Public class Producersandconsumers {
  7. public static void Main (string[] args) {
  8. Storage Storage = new Storage ();
  9. Thread consumer = New Thread (new consumer (storage));
  10. Consumer.setname ("consumer");
  11. Thread producer = new Thread (new producer (storage));
  12. Producer.setname ("producer");
  13. Consumer.start ();
  14. Producer.start ();
  15. }
  16. }
  17. /**
  18. * Consumer
  19. */
  20. Class Consumer implements Runnable {
  21. private Storage Storage;
  22. Public Consumer (Storage Storage) {
  23. this.storage = storage;
  24. }
  25. @Override
  26. public Void Run () {
  27. Storage.pop ();
  28. }
  29. }
  30. /**
  31. * Producer
  32. */
  33. Class Producer implements Runnable {
  34. private Storage Storage;
  35. Public Producer (Storage Storage) {
  36. this.storage = storage;
  37. }
  38. @Override
  39. public Void Run () {
  40. Product Product = New Product ("090505105", "phone");
  41. Storage.push (product);
  42. }
  43. }
  44. /**
  45. * Product Category
  46. */
  47. Class Product {
  48. private String ID; //Product ID
  49. private String name; //Product name
  50. Public Product (string ID, string name) {
  51. this.id = ID;
  52. this.name = name;
  53. }
  54. @Override
  55. Public String toString () {
  56. return "(Product ID:" + ID + "product Name:" + name + ")";
  57. }
  58. Public String getId () {
  59. return ID;
  60. }
  61. public void SetId (String id) {
  62. this.id = ID;
  63. }
  64. Public String GetName () {
  65. return name;
  66. }
  67. public void SetName (String name) {
  68. this.name = name;
  69. }
  70. }
  71. /**
  72. * Warehouse
  73. */
  74. Class Storage {
  75. //Warehouse capacity is ten
  76. private product[] products = new product[10];
  77. private int top = 0;
  78. //producers put products into the warehouse
  79. public synchronized void push (product product) {
  80. While (top = = products.length) {
  81. try {
  82. Wait (); //Warehouse is full, waiting for
  83. } catch (Interruptedexception e) {
  84. //TODO auto-generated catch block
  85. E.printstacktrace ();
  86. }
  87. }
  88. //Put the product into the warehouse
  89. products[top++] = product;
  90. System.out.println (Thread.CurrentThread (). GetName () + "produced products"
  91. + product);
  92. Notifyall (); //wake-up waiting thread
  93. }
  94. //Consumers remove products from the warehouse
  95. public Synchronized Product pop () {
  96. While (top = = 0) {
  97. try {
  98. Wait (); //Warehouse empty, waiting
  99. } catch (Interruptedexception e) {
  100. //TODO auto-generated catch block
  101. E.printstacktrace ();
  102. }
  103. }
  104. //Pick up the product from the warehouse
  105. --top;
  106. Product P = New Product (Products[top].getid (), Products[top].getname ());
  107. Products[top] = null;
  108. System.out.println (Thread.CurrentThread (). GetName () + "Consumption of products" + p);
  109. Notifyall (); //wake-up waiting thread
  110. return p;
  111. }
  112. }
  113. </span>


Operation Result:

The producer produced the product (product id:090505105 Product Name: Telephone)
Consumer consumption of products (product id:090505105 Product Name: Telephone)

Java Multithreading Summary Six: the classic producer consumer problem realization

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.