Producer Consumer
Producers create data, control traffic through intermediaries, and deliver it securely to consumers.
Applicable environment
The speed of producer production data is inconsistent with the speed with which consumers process data, and intermediaries adjust the data pressure of consumers by caching and blocking.
Sample Example
4 producers produce products, put into the market, 2 consumer consumption.
Products
Package Producerconsumer;public class Product {private String Prdid=null;public product (String prdid) {This.prdid=prdid ;} Public String Getorderid () {return this.prdid;}}
Market
Package Producerconsumer;import Java.util.linkedlist;public class Mart {private final linkedlist<product> items= New Linkedlist<product> ();p rivate final int size;private int state=0;public Mart (int size) {this.size=size;} Public synchronized void put (Product PRD) {while (Items.size () >=size) {try {wait]} catch (Interruptedexception e) {}}items.add (PRD); Notifyall ();} Public synchronized Product get () {while (Items.size () <=0&&state==0) {try {wait ();} catch ( Interruptedexception e) {}}product ret=null;ret=items.poll (); Notifyall (); return ret;} Public synchronized Boolean isEmpty () {if (Items.size () ==0) {return true;} return false;} Public synchronized void setState (int st) {This.state=st;}}
Producers
Package Producerconsumer;import Java.util.uuid;public class Producer implements Runnable{private final String MyName; Private final Mart mart;private int state=0;public Producer (String myname,mart Mart) {This.myname=myname;this.mart=mart ;} @Overridepublic void Run () {while (state==0) {Product p=new product (Uuid.randomuuid (). toString ()); Mart.put (p); try { Thread.Sleep (100);} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println (this.myname+ "produced Products:" +p.getorderid () + "has entered the market. ");} System.out.println (this.myname+ "has stopped production. ");} public void setState (int st) {This.state=st;}}
Consumers
Package Producerconsumer;public class Consumer implements Runnable {private final String myname;private final Mart MART;PR ivate int state=0;public Consumer (String myname,mart Mart) {This.myname=myname;this.mart=mart;} @Overridepublic void Run () {while (state==0) {Product p=mart.get (); if (p!=null) {try {thread.sleep ($);} catch ( Interruptedexception e) {e.printstacktrace ();} System.out.println (this.myname+ "Consumed the Product:" +p.getorderid () + ". ");}} System.out.println (this.myname+ "has stopped spending. ");} public void setState (int st) {This.state=st;}}
Test class
Package producerconsumer;public class test {public static void main (String[ ] args) {system.out.println ("mainline begins. "); Mart m=new mart (10); Producer p1=new producer ("P1", m); Producer p2=new producer ("P2", m); Producer p3=new producer ("P3", m); Producer p4=new producer ("P4", M); Consumer c1=new consumer ("C1", m); Consumer c2=new consumer ("C2", m); Thread ctd1=new thread (C1); Thread ctd2=new thread (C2); Thread ptd1=new thread (p1); Thread ptd2=new thread (p2); Thread ptd3=new thread (p3); Thread ptd4=new thread (p4);p Td1.start ();p Td2.start ();p Td3.start ();p td4.start (); try { System.out.println ("Main thread sleeps for 5 seconds. "); Thread.Sleep (5000); SYSTEM.OUT.PRINTLN ("The main thread sleeps over. "); SYSTEM.OUT.PRINTLN ("The market reaches saturation, producers are blocked and consumers are opened.") "); Ctd1.start (); Ctd2.start (); SYSTEM.OUT.PRINTLN ("The main thread sleeps for 5 seconds again.") "); Thread.Sleep (5000); SYSTEM.OUT.PRINTLN ("The main thread sleeps over. ");} catch (Interruptedexception e) {e.printstacktrace ();} System.out.println ("Set up producer poison pills.") ");p 1.setState ( -1);p 2.setState ( -1);p 3.setState ( -1);p 4.setState (-1); System.out.println ("Wait for the queue to empty. "); while (true) {if (!m.isempty ()) {try {thread.sleep (1000);} catch (interruptedexception e) {e.printstacktrace ();}} Else{break;}} System.out.println ("The queue has been emptied. "); M.setstate (-1); System.out.println ("Set up consumer poison pills.") "); C1.setstate ( -1); C2.setstate (-1);}}
This article is from the Java Technology Stack notes blog, so be sure to keep this source http://stroll.blog.51cto.com/11038467/1856913
Java Multithreading (vii) mode-producer Consumer