Synchronous function producer and consumer model enhancement (multi-person production and multi-person consumption)

Source: Internet
Author: User

has been engaged for a long time, the producer and consumer model strengthened version (multi-person production multi-person consumption).

The previous code format is no longer a slot (try to improve later)

The output is an infinite loop

Import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import java.util.concurrent.locks.reentrantlock;/** * Multiple producers & multiple consumer models * Multiple producers continue to produce, multiple consumers are constantly consuming */public class    LockConditionDemo1 {public static void main (string[] args) {Resource2 r = new Resource2 ();    Producer2 p = new Producer2 (r);    Consumer2 C = new Consumer2 (r);    New Thread (P, "producer 0"). Start ();    New Thread (P, "producer 1"). Start ();    New Thread (P, "producer 2"). Start ();    New Thread (P, "producer 3"). Start ();    New Thread (C, "Consumer 4"). Start ();    New Thread (C, "Consumer 5"). Start ();    New Thread (c, "Consumer 6"). Start (); New Thread (c, "Consumer 7"). Start ();}}  Create a data source class resource2{private String name;  private int num=1;  Public String GetName () {return name;  } public void SetName (String name) {this.name = name;  } public int Getnum () {return num;  } Lock Lock = new Reentrantlock ();  Create Condition Con_pro = Lock.newcondition ();  Condition Con_con = Lock.newcondition ();  Condition c= lock.newcondition (); Create a tag BooleAn flag = False;//true, Fasle no int[] arr= new int[5];  int start,end,count; Producers continue to produce public void produce (throws Interruptedexception{thread.sleep), Lock.lock (); try{while (count== Arr.length) {//here to be judged with while, otherwise the probability of error//con_pro.await (); c.await ();} Arr[start] = x; if (++start = = arr.length) start = 0;//this.setname ("Roast Duck" +num); This.setname ("Roast Duck");//system.out.println ( Thread.CurrentThread (). GetName () + "---producer produced:" +this.getname ()); System.out.println (Thread.CurrentThread (). GetName () + "-Producer produced:" + (count+1)); Num++;++count;//flag = True;//con_ Con.signalall (); C.signalall ();} Finally{lock.unlock ();}} Consumption of the constant consumption of public void consume () throws Interruptedexception{thread.sleep () Lock.lock (); Try{while (count = = 0) {// Con_con.await (); c.await ();} if (++end = = arr.length) end = 0;//this.setname ("Roast Duck" +num);//cannot have//or num will increase by one. Because the production of num++//system.out.println ( Thread.CurrentThread (). GetName () + "=========== Consumer consumption:" +this.getname ()); System.out.println (Thread.CurrentThread (). GetName () + "= = Consumer Consumption:" +count);//num++;//cannot increase--count;//flag = False;//con_pro.signalall (); C.signalall ();} Finally{lock.unlock ();}}} Producer Class Producer2 implements RUNNABLE{RESOURCE2 R; Producer2 (Resource2 R) {this.r=r;} @Overridepublic void Run () {while (true) {//here to be judged with while, otherwise the probability of error will occur if the try {r.produce ();} catch (Interruptedexception e) { E.printstacktrace ();}}} Consumer class Consumer2 implements RUNNABLE{RESOURCE2 R; Consumer2 (Resource2 R) {this.r=r;} @Overridepublic void Run () {while (true) {try {r.consume ()} catch (Interruptedexception e) {e.printstacktrace ();}}}}

=============== Results ==================

Producer 0-producer produced: 1
Producer 2-producer produced: 2
Producer 3-producer produced: 3
Consumer 6==== Consumer consumption: 3
Consumer 7==== consumer consumption: 2
Producer 1-producer produced: 2
Consumer 4==== consumer consumption: 2
Consumer 5==== consumer consumption: 1
Producer 0-producer produced: 1
Consumer 6==== consumer consumption: 1
Producer 3-producer produced: 1
Producer 2-producer produced: 2
Consumer 7==== consumer consumption: 2
Producer 1-producer produced: 2
Consumer 5==== consumer consumption: 2
Consumer 4==== consumer consumption: 1
Producer 0-producer produced: 1
Consumer 6==== consumer consumption: 1
Producer 3-producer produced: 1
Producer 2-producer produced: 2

  

Synchronous function producer and consumer model enhancement (multi-person production and multi-person consumption)

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.