Blockingqueue implements producer-consumer model

Source: Internet
Author: User

The producer-consumer model serves as a bridge between the buffer zone and the producer only needs to be responsible for the production and put the produced messages into the buffer zone, regardless of the number of producers and consumers, consumers only need to take out messages in the buffer zone for consumption, and do not need to worry about the number of consumers and producers.

While blockingqueueIt solves the problem of how to pass messages collected by one thread to another thread for processing, and does not need to consider synchronization.

Below isCodeImplementation:

 1   /**  2   *  3    */  4   Package  Bells;  5   6   Import  Java. util. arrays; 7   Import  Java. util. List;  8   Import  Java. util. Concurrent. arrayblockingqueue;  9   Import  Java. util. Concurrent. blockingqueue;  10   11   /**  12   * Producer  13   * 14   *  @ Author  Bellszhu  15   *  16    */  17   Class Procuder Implements  Runnable {  18   19       Private Blockingqueue <string> Queue; 20   21       Public Procuder (blockingqueue <string> Queue ){  22           This . Queue = Queue;  23   }  24   25   @ Override  26       Public   Void  Run (){ 27           Try  {  28               For  (String message: producemessages ()){  29   Queue. Put (Message );  30   }  31 } Catch  (Interruptedexception e ){  32 System. Err. println ("interrupted! "+E. getmessage ());  33   }  34   }  35   36       /**  37   * Simple production process  38   *  39   *  @ Return  40        */ 41       Private List <string> Producemessages (){  42           Return Arrays. aslist ("I'm Coming", "I'm done it", "I'm win" ,  43 "Congratulate", "over" );  44   }  45   }  46   47   /** 48   * Consumer  49   *  50   *  @ Author  Bellszhu  51   *  52    */  53   Class Consumer Implements  Runnable {  54  55       Private Blockingqueue <string> Queue;  56   57       Public Consumer (blockingqueue <string> Queue ){  58           This . Queue = Queue;  59   }  60   61   @ Override 62       Public   Void  Run (){  63           Try  {  64 String message = Null  ;  65               While (! (Message = queue. Take (). Equals ("over" ))){  66                   //  Consumers process messages 67 System. Out. println ("thread:" + Thread. currentthread (). GETID ()  68 + "\ Tmessage:" + Message );  69   }  70 } Catch  (Interruptedexception e ){  71 System. Err. println ("interrupted! "+ E. getmessage ());  72   } 73   }  74   }  75   76   /**  77   *  @ Author  Bellszhu  78   *  79    */  80   Public   Class Procuderconsumertest {  81       Public   Static   Void  Main (string [] ARGs ){  82 Blockingqueue <string> queue = New Arrayblockingqueue <string> (1, True  );  83   84           New Thread ( New Procuder (Queue). Start ();  85   86           New Thread ( New  Consumer (Queue). Start ();  87           New Thread ( New  Consumer (Queue). Start ();  88   }  89 }

The Design of producer-consumer model can be referred to: http://blog.csdn.net/program_think/article/details/4022087

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.