Dark Horse programmer--java--Communication between threads producer and consumer

Source: Internet
Author: User



Communication between the threads producer and consumer

public class Testdemos3{public static void Main (string[] args) {res  r = new Res (), Input in = new input (r), Output out = New Output (R); thread T1 = new thread (in); Thread t2 = new Thread (out); T1.start (); T2.start ();}} class res{string name; String sex;} Class Input  implements Runnable{private Object obj;private Res r;input (res r) {this. R = r;} public void Run () {Boolean b = False;while (true) {    synchronized (input.class)    {       if (b)       {        r.name= "Zhang San ";        r.sex= "male";        B=false;       }       else       {       r.name = "KK";       R.sex = "Woman";        B=true;}}}}    Class Output implements runnable{    private Res R;    Private Object obj;    Output (Res R)    {    THIS.R = R;    }    public void Run ()    {while    (true)    {    synchronized (input.class)    {        System.out.println ( Thread.CurrentThread (). GetName () +r.name+ ":" +r.sex);}}}    




Wait for wake-up mechanism

public class Testdemos3{public static void Main (string[] args) {res r = new Res (), Input in = new input (r), Output out = new Output (R); thread T1 = new thread (in); thread t3 = new thread (in); Thread t2 = new Thread (out); thread T4 = new thread (out); T1.start (); T2.start (); T3.start (); T4.start ();}} class res{string name; String Sex;boolean flag = false;} Class Input implements Runnable{private Object obj;private Res r;input (res r) {this. R = r;} public void Run () {Boolean b = False;while (true) {synchronized (R) {if (R.flag) {try {r.wait ();} catch (Inter    Ruptedexception e) {e.printstacktrace ();}        } if (b) {r.name= "Zhang San";        r.sex= "Male";       B=false;       } else {r.name = "KK";        R.sex = "Woman";       B=true;       } r.flag=true;    R.notify ();    }}}}class Output implements runnable{private Res R;    Private Object obj;    Output (Res r) {THIS.R = R; public void Run () {while (true) {synchronized (R)   {if (!r.flag) {try {r.wait ();} catch (Interruptedexception e) {e.printstacktrace ();}                } System.out.println (Thread.CurrentThread (). GetName () +r.name+ ":" +r.sex);                R.flag=false;    R.notify (); }    }    }}

To run the program:





Multithreaded demo producer and consumer examples


public class Produceconsumdemos {public static void main (string[] args) {Ress r = new Ress (), Inputs in = new Inputs (r); Puts out = new Outputs (r); Thread t = new thread (in); thread T1 = new thread (in); Thread t2 = new Thread (out); thread t3 = new thread (out); T.start (); T1.start (); T2.start (); T3.start ();}} Class Ress{private String Name;private boolean blag = false;private int count=0;public synchronized void SetInput (string n AME) {while (blag) {try {wait ();} catch (Interruptedexception e) {e.printstacktrace ()}} THIS.name = name+ "--" +count++; System.out.println (Thread.CurrentThread (). GetName () + "producer-" +this.name); Blag=true;this.notifyall (); Public synchronized void out () {while (!blag) {try {wait ()} catch (Interruptedexception e) {e.printstacktrace ()}} System.out.println (Thread.CurrentThread (). GetName () + "Consumer---" +this.name); Blag=false;this.notifyall ();}} Class Inputs implements Runnable{private Ress r;inputs (Ress R) {this.r=r;} public void Run () {while (true) {r.setinput ("Commodity");}}} Class Outputs Implements Runnable{private Ress r;outputs (Ress r) {THIS.R = R;} public void Run () {while (true) R.out ();}}




Join lock

Import Java.util.concurrent.locks.condition;import Java.util.concurrent.locks.lock;import Java.util.concurrent.locks.reentrantlock;public class Test3 {public static void main (string[] args) { System.out.println ("Text3 start"); resii r = new Resii (), inputii in = new Inputii (r), outputii out = new Outputii (r); thread T1 = new thread (in); Thread t2 = new Thread (in); thread t3 = new thread (out); thread T4 = new thread (out); T1.start (); T2.start (); T3.start (); T4.start ();}} Class Resii{private String name;p rivate boolean flag = false;private int count=0;private lock lock = new Reentrantlock (); Private Condition Conin = Lock.newcondition ();p rivate Condition conout = lock.newcondition ();p ublic void Set (String name) {Lock.lock (); while (flag) {try {conin.await ()} catch (Interruptedexception e) {e.printstacktrace ();}} THIS.name = name+ ":" +count++; System.out.println (Thread.CurrentThread (). GetName () + "producer" +this.name); flag = True;conout.signal (); Lock.unlock ();} public void Out () {Lock.lock (), while (!flag) {try {conout.aWait ();} catch (Interruptedexception e) {e.printstacktrace ();}} System.out.println (Thread.CurrentThread (). GetName () + "consumer ________" +this.name); flag = False;conin.signal (); Lock.unlock ();}} Class Inputii implements Runnable{private resii r;inputii (resii R) {this.r=r;} public void Run () {while (true) {R.set ("Commodity");}}}   Class Outputii implements runnable{private resii R;   Outputii (resii r) {THIS.R = R;   public void Run () {while (true) {r.out (); }   }}











Dark Horse programmer--java--Communication between threads producer and consumer

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.