Java multithreading-producer consumer Mode

Source: Internet
Author: User

This is a common mode for Java Development. It allows the program to call each other between producers and consumers during design.

 

Three objects need to be created in the mode: producer consumer warehouse

Producer: Put the products produced into the total warehouse

Consumer: extracts products from the warehouse for consumption.

Warehouse: A synchronization method is used to lock and synchronize the warehouse. Only one object can be operated on at a time.

 

 

The class diagram is as follows:

 

 

 

Repository code: queue-based implementation

 

Package consumer; </P> <p> Import Java. util. concurrent. arrayblockingqueue; </P> <p> Import Org. slf4j. logger; <br/> Import Org. slf4j. loggerfactory; </P> <p> public class serveralarmqueue {<br/> Private Static int max_size = 1000; <br/> Private Static logger = loggerfactory. getlogger (serveralarmqueue. class); <br/> Private Static arrayblockingqueue <Object> alramqueue = NULL; <br/> Public serveralarmqueue (INT capacity) {<br/> super (); <br/> max_size = capacity; <br/> alramqueue = new arrayblockingqueue <Object> (capacity); <br/>}</P> <p> Public serveralarmqueue () {<br/> super (); <br/> alramqueue = new arrayblockingqueue <Object> (max_size ); <br/>}</P> <p>/** <br/> * Get an object from the end of the queue <br/> * @ return <br /> */<br/> Public synchronized object consume () {<br/> Object object = NULL; <br/> while (getqueuesize () = 0) {<br/> try {<br/> wait (); <br/> logger. debug ("the queue is empty, consumption wait... "); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> Object = alramqueue. poll (); <br/> system. out. println (thread. currentthread (). getname () + "consumption product:" + object. tostring (); <br/> This. policyall (); <br/> return object; <br/>}</P> <p>/** <br/> * Insert an object to another object <br/> * @ Param object <br/> * @ return <br/> */<br/> Public synchronized Boolean produce (Object object) {<br/> Boolean flag = false; <br/> while (getqueuesize () = max_size) {<br/> try {<br/> wait (); <br/> logger. debug ("the queue is full, production Waits .... "); <br/>} catch (interruptedexception e) {<br/> E. printstacktrace (); <br/>}< br/> flag = alramqueue. offer (object); <br/> system. out. println (thread. currentthread (). getname () + "produce product:" + object. tostring (); <br/> This. policyall (); <br/> return flag; <br/>}</P> <p> Public int getqueuesize () {<br/> return alramqueue. size (); <br/>}</P> <p> Public void clear () {<br/> alramqueue. clear (); <br/>}</P> <p >}< br/>

 

Consumer:

 

Package consumer; </P> <p> public class Consumer implements runnable {<br/> private serveralarmqueue = NULL; <br/> private Boolean isstart = true; </P> <p> public consumer (serveralarmqueue) {<br/> super (); <br/> This. serveralarmqueue = serveralarmqueue; <br/>}</P> <p> @ override <br/> Public void run () {<br/> while (isstart) {<br/> getproducts (); <br/> try {</P> <p> // wait for 30 ms <br/> thread. sleep (30); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> private void getproducts () {<br/> Object OBJ = serveralarmqueue. consume (); <br/> system. out. println (OBJ); <br/>}</P> <p> Public void stop () {<br/> isstart = false; <br/>}< br/>

 

Producer:

 

Package COM. consume; </P> <p> public class producer implements runnable {<br/> private serveralarmqueue = NULL; </P> <p> Public producer (serveralarmqueue) {<br/> super (); <br/> This. serveralarmqueue = serveralarmqueue; <br/>}</P> <p> @ override <br/> Public void run () {<br/> char C; </P> <p> for (INT I = 0; I <200; I ++) {</P> <p> // generate random uppercase letters <br/> C = (char) (math. random () * 26 + 'A'); <br/> produce (C ); </P> <p> try {</P> <p> // wait for 30 milliseconds <br/> thread. sleep (30); <br/>}catch (exception e) {<br/> E. printstacktrace (); <br/>}</P> <p> private void produce (Object OBJ) {<br/> serveralarmqueue. produce (OBJ); <br/>}< br/>

 

 

 

 

Test code

 

 

Package COM. consume; </P> <p> public class threadmain {</P> <p> Public static void main (string [] ARGs) {<br/> // 1. prepare the stack data structure <br/> serveralarmqueue stack = new serveralarmqueue (); </P> <p> // 2. prepare the producer thread <br/> producer producer1 = new producer (stack); <br/> thread T1 = new thread (producer1 ); <br/> t1.setname ("producer 1"); </P> <p> producer producer2 = new producer (stack ); <br/> thread t2 = new thread (producer2); <br/> t2.setname ("producer 2"); </P> <p> // 3. prepare the consumer thread <br/> consumer consumer1 = new consumer (stack); <br/> thread T3 = new thread (consumer1 ); <br/> t3.setname ("Consumer 1"); </P> <p> consumer consumer2 = new consumer (stack ); <br/> thread t4 = new thread (consumer2); <br/> t4.setname ("Consumer 2"); </P> <p> t3.start (); <br/> t4.start (); </P> <p> t1.start (); <br/> t2.start (); <br/>}< br/>

 

 

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.