Multi-threading and multi-producer multiple consumer realization problem

Source: Internet
Author: User

Multi-producer Multi-consumer is a classic case in Java, implemented by the wait-wake mechanism, the code is as follows:

  

 Public classProducerconsumer { Public Static voidMain (string[] args) {Resource res=NewResource (); Producer Pro1=NewProducer (RES); Producer Pro2=NewProducer (RES); Consumer Con1=NewConsumer (RES); Consumer Con2=NewConsumer (RES); Thread T1=NewThread (Pro1); Thread T2=NewThread (PRO2); Thread T3=NewThread (Con1); Thread T4=NewThread (Con2);        T1.start ();        T2.start ();        T3.start ();    T4.start (); }}classResource {Private intcount; Private Booleanflag=true;  Public synchronized voidProduce () { while(!flag) {            Try {                 This. Wait (); } Catch(interruptedexception e) {e.printstacktrace (); }} System.out.println (Thread.CurrentThread (). GetName ()+ "Production" +count+ "Roast Duck"); Count++; Flag=false;  This. Notifyall (); }     Public synchronized voidconsume () { while(flag) {Try {                 This. Wait (); } Catch(interruptedexception e) {e.printstacktrace (); }} System.out.println (Thread.CurrentThread (). GetName ()+ "consumption" +count+ "Roast Duck"); Flag=true;  This. Notifyall (); }}classProducerImplementsrunnable{Resource R;  PublicProducer (Resource r) {Super();  This. R =R; } @Override Public voidrun () { while(true) {r.produce (); }    }    }classConsumerImplementsRunnable {Resource R;  PublicConsumer (Resource r) {Super();  This. R =R; } @Override Public voidrun () { while(true) {r.consume (); }    }    }

There is also a multi-producer multi-consumer model implemented with Blockqueue:

ImportJava.util.concurrent.BlockingQueue;ImportJava.util.concurrent.LinkedBlockingQueue; Public classProducerconsumer { Public Static voidMain (string[] args) {Resource res=NewResource (); Blockingqueue Queue=NewLinkedblockingqueue (1); Producer Pro1=NewProducer (Res,queue); Producer Pro2=NewProducer (Res,queue); Consumer Con1=NewConsumer (Res,queue); Consumer Con2=NewConsumer (Res,queue); Thread T1=NewThread (Pro1); Thread T2=NewThread (PRO2); Thread T3=NewThread (Con1); Thread T4=NewThread (Con2);        T1.start ();        T2.start ();        T3.start ();    T4.start (); }}classResource {intcount; @Override PublicString toString () {return"Food [count=" + Count + "]"; }    }classProducerImplementsrunnable{Resource R;        Blockingqueue queue;  PublicProducer (Resource R, Blockingqueue queue) {Super();  This. R =R;  This. Queue =queue; } @Override Public voidrun () { while(true) {produce (); }    }     Public voidProduce () {Try{System.out.println ("Production" +R);            Queue.put (R); R.count++; } Catch(interruptedexception e) {e.printstacktrace (); }    }    }classConsumerImplementsRunnable {Resource R;        Blockingqueue queue;  PublicConsumer (Resource R, Blockingqueue queue) {Super();  This. R =R;  This. Queue =queue; } @Override Public voidrun () { while(true) {consume (); }    }     Public voidconsume () {Try{System.out.println ("Consumption" +Queue.take ()); } Catch(interruptedexception e) {e.printstacktrace (); }    }    }

Multi-threading and multi-producer multiple consumer realization problem

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.