Interesting producer and consumer problems

Source: Internet
Author: User

Reprinted please indicate the source: http://blog.csdn.net/sunyujia/

The questions raised by netizens on the Forum can only be consumed for each 3 production sites. It is not difficult to do this for the sake of convenience.

Modified on the basis of the original http://blog.csdn.net/sunyujia/archive/2008/05/02/2362015.aspx.

The only difficulty is that three production threads are not the same. The two threads consumed are not the same thread. That is to say, it cannot be a thread that produces three in a row, and one thread consumes two in a row. If it is a thread that produces continuously, the program will be boring.

  1. Import java. util. arraylist;
  2. Import java. util. List;
  3. Import java. util. Concurrent. executorservice;
  4. Import java. util. Concurrent. executors;
  5. /**
  6. * Interesting producer and consumer problems
  7. * Two shards are consumed for each generation of three shards.
  8. * @ Author: sunyujia
  9. * @ Blog: http://blog.csdn.net/sunyujia/
  10. * @ Mail: sunyujia @ sunyujia@yahoo.cn
  11. */
  12. Public class executorservicetest {
  13. Private Static int producecount = 0;
  14. Private Static int consumecount = 0;
  15. Private Static object producelock = new object ();
  16. Private Static object consumelock = new object ();
  17. Public static void main (string [] ARGs ){
  18. Final list tasks = new arraylist ();
  19. System. Out. println ("myblog: http://blog.csdn.net/" + "sunyujia /");
  20. Final executorservice exec = executors. newcachedthreadpool ();
  21. For (INT I = 0; I <10; I ++) {// 10 producers
  22. Exec.exe cute (New runnable (){
  23. Public void run (){
  24. While (! Thread. interrupted ()){
  25. Try {
  26. Synchronized (consumelock ){
  27. If (producecount> = 3)
  28. Consumelock. Wait ();
  29. }
  30. Synchronized (producelock ){
  31. If (producecount <3 ){
  32. Tasks. Add (new object (); // Production
  33. System. Out. println (thread. currentthread (). getname () + "remaining production tasks:" + tasks. Size ());
  34. Producecount ++;
  35. If (producecount> = 3 ){
  36. Consumecount = 2; // you are prompted to consume only two
  37. System. Out. println ("producer reminder inventory now"
  38. + Tasks. Size ());
  39. Producelock. policyall ();
  40. }
  41. Thread. Yield ();
  42. }
  43. }
  44. Thread. Sleep (600); // Let the program run slowly for observation
  45. } Catch (interruptedexception e ){
  46. E. printstacktrace ();
  47. }
  48. }
  49. }
  50. });
  51. }
  52. For (INT I = 0; I <20; I ++) {// 20 consumers
  53. Exec.exe cute (New runnable (){
  54. Public void run (){
  55. While (! Thread. interrupted ()){
  56. Try {
  57. Synchronized (producelock ){
  58. If (consumecount <= 0)
  59. Producelock. Wait ();
  60. }
  61. Synchronized (consumelock ){
  62. If (consumecount> 0 ){
  63. Tasks. Remove (0); // consume
  64. System. Out. println (thread. currentthread (). getname () + "remaining tasks :"
  65. + Tasks. Size ());
  66. Consumecount --;
  67. If (consumecount <= 0 ){
  68. Producecount = 0; // three more are required
  69. System. Out. println ("Consumer reminder inventory now"
  70. + Tasks. Size ());
  71. Consumelock. policyall ();
  72. }
  73. }
  74. }
  75. Thread. Sleep (600); // Let the program run slowly for observation
  76. } Catch (interruptedexception e ){
  77. E. printstacktrace ();
  78. }
  79. }
  80. }
  81. });
  82. }
  83. Exec. Shutdown ();
  84. }
  85. }

The execution result is as follows:
Pool-1-thread-1 production task remaining: 1
Pool-1-thread-2 production task remaining: 2
Pool-1-thread-4 production task remaining: 3
Producer friendly reminder current inventory 3
Pool-1-thread-11: 2
Pool-1-thread-12: 1
Consumer friendly reminder current inventory 1
Pool-1-thread-5 production task remaining: 2
Pool-1-thread-6 production task remaining: 3
Pool-1-thread-7 production task left: 4
Producer reminder inventory 4
Pool-1-thread-14: 3
Pool-1-thread-16 remaining tasks: 2
Consumer reminder inventory 2
Pool-1-thread-3 production task remaining: 3
Pool-1-thread-2 production task left: 4
Pool-1-thread-4 production task remaining: 5
Producer friendly reminder inventory 5

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.