Java implementations of producer consumers

Source: Internet
Author: User

First, the simplest, that is, the buffer capacity is 1.

Buffer capacity of 1
Import Java.util.list;public class ProducerAndConsumer2 {static Class Addthread implements Runnable {Plate pla        Te        Public Addthread (Plate p) {this.plate = P; } @Override public void Run () {//TODO auto-generated Method Stub plate.put (new Objec        T ());        }} Static Class GetThread implements Runnable {Plate Plate;        Public GetThread (Plate p) {this.plate = P;        } @Override public void Run () {//TODO auto-generated Method Stub plate.get ();        }} static class Plate {private list<object> egg = new arraylist<object> (); Public synchronized void put (Object o) {while (egg.size () > 0) {try {Sy                    Stem.out.println ("There is egg production line in the plate" +thread.currentthread (). GetName () + "blocking");                This.wait (); } catch (InterruptedeXception e) {//TODO auto-generated catch block E.printstacktrace ();            }} egg.add (0, O);            SYSTEM.OUT.PRINTLN ("Production line into an egg");        This.notifyall ();                    } public synchronized Object get () {while (egg.size () = = 0) {try {                    System.out.println ("There is no egg consumption thread in the plate" +thread.currentthread (). GetName () + "blocking");                This.wait (); } catch (Interruptedexception e) {//TODO auto-generated catch block e.printstacktr                Ace ();            }} Object object = Egg.get (0);            Egg.clear ();            SYSTEM.OUT.PRINTLN ("Consumer thread gets an egg");            This.notifyall ();        return object;        }} public static void Main (string[] args) {Plate p = new Plate (); for (int i = 0; i < ten; i++) New Thread (NEW GetThread (P)). Start ();    for (int i = 0; i < i++) New Thread (New Addthread (p)). Start (); }}



If you can store more than one egg on a plate:

Buffer capacity greater than 1
Package Cn.itcast.heima2;import Java.util.arraylist;import Java.util.list;public class Producerandconsumer {public St                atic void Main (string[] args) {Plate p = new Plate ();        for (int i = 0; i < i++) New Thread (New GetThread (p)). Start ();    for (int i = 0; i < i++) New Thread (New Addthread (p)). Start ();    }}class Addthread implements Runnable {Plate Plate;    Public Addthread (Plate p) {this.plate = P;    } @Override public void Run () {//TODO auto-generated Method Stub plate.put (new Object ());    }}class GetThread implements Runnable {Plate Plate;    Public GetThread (Plate p) {this.plate = P;    } @Override public void Run () {//TODO auto-generated Method Stub plate.get ();                                   }}class Plate {private list<object> egg = new arraylist<object> (10);        In 0-9 of these 10 spaces storage eggs public synchronized void put (Object o) {while (Egg.size () ==10) {//At most there is an egg try {System.out.println ("There is an egg on the plate                Production line "+thread.currentthread (). GetName () +" blocking ");            This.wait ();            } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();        }} egg.add (O);        SYSTEM.OUT.PRINTLN ("Production line into an egg existing egg" +egg.size () + "one");    This.notifyall ();                         The public synchronized an Object get () {while (Egg.size () ==0) {try {System.out.println (                "There is no egg consumption thread in the plate" +thread.currentthread (). GetName () + "blocking");            This.wait ();            } catch (Interruptedexception e) {//TODO auto-generated catch block E.printstacktrace ();        }} Object Object=egg.get (Egg.size ()-1);        Egg.remove (Egg.size ()-1);        SYSTEM.OUT.PRINTLN ("Consumer thread gets an egg with the remaining eggs" +egg.size () + "a"); ReturnObject }}

Thanks GLT
Reference http://blog.csdn.net/ghsau/article/details/7433673
http://blog.csdn.net/monkey_d_meng/article/details/6251879

Java implementations of producer consumers

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.