The waiting wake mechanism of communication between multiple threads

Source: Internet
Author: User

Inter-thread communication:
The fact is that multiple threads are manipulating the same resource.
But the action is different.

Examples:

Requirements: Analog Simple ticketing system (enter a person. followed by the output of a person)

class res{string name; String sex;} Class Input  implements runnable{private res r;private int T=0;input (res r) {this.r=r;} public void Run () {while (true) {if (t==1) {r.name= "Nike"; r.sex= "man";} else {r.name= "Lili"; r.sex= "female";} t= (t+1)%2;}} Class Output  implements Runnable{private Res R;output (res r) {this.r=r;} public void Run () {while (true) {System.out.println ("Output ..." +r.name+ "+ + +" +r.sex);}} Class Inputoutputdemo{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 ();}}

A security issue has occurred (output Lili man)

After synchronization

class res{string name; String sex;} Class Input  implements runnable{private res r;private int T=0;input (res r) {this.r=r;} public void Run () {while (true) {synchronized (Res.class) {if (t==1) {r.name= "Nike"; r.sex= "man";} else {r.name= "Lili"; r.sex= "female";} t= (t+1)%2;}}} Class Output  implements Runnable{private Res R;output (res r) {this.r=r;} public void Run () {synchronized (true) {Res.class} {System.out.println ("Output ..." +r.name+ "+ + +" +r.sex);}}} Class Inputoutputdemo2{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 ();}}


Despite the security problem, there was no alternate scene between a man and a woman we wanted.

This is the introduction of a method: waiting for the wake mechanism

class res{string name; String Sex;boolean Flag; Class Input  implements runnable{private res r;private int T=0;input (res r) {this.r=r;} public void Run () {while (true) {synchronized (R) {if (R.flag) try{r.wait ();} catch (Exception e) {}if (t==1) {r.name= "Nike"; r.sex= "man";} else {r.name= "Lili"; r.sex= "female";} t= (t+1)%2;r.flag=true;r.notify ();}}} Class Output  implements Runnable{private Res R;output (res r) {this.r=r;} public void Run () {while (true) {synchronized (R) {if (!r.flag) try{r.wait ();} catch (Exception e) {}system.out.println ("Output ..." +r.name+ "+ + +" +r.sex); R.flag=false;r.notify ();}}} Class Inputoutputdemo3{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 ();}}

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /center ">


Wait
Notify ();
Notifyall ();

are used in synchronization because of the operation to hold the monitor (lock).
So to use in synchronization, there is a lock because there is only synchronization.

Why do these operations threads of Phoenix fly to define the object class?
Because these methods are synchronized during the operation of the threads. are required to identify the locks in the thread they are operating on,
Only the same locked thread is waiting. Can be awakened by the same lock notify.
Cannot be awakened by a thread in a different lock.

In other words, wait and wake must be the same lock.


A lock can be a discretionary object, so it can be defined in the object class by a method called by a random object.

The following code improvements are made:

Class Res{private String Name;private string sex;private boolean flag;public synchronized void set (string name,string sex ) {if (This.flag) try{this.wait ();}  catch (Exception e) {}this.name=name;this.sex=sex;this.flag=true; This.notify ();} Public synchronized void out () {if (!this.flag) try{this.wait ();} catch (Exception e) {}system.out.println ("Output ..." +this.name+ "+ + +" +this.sex); This.flag=false;this.notify ();}} Class Input implements runnable{private res r;private int T=0;input (res r) {this.r=r;} public void Run () {(true) {synchronized (R) {if (t==1) r.set ("Nike", "man"), Else R.set ("Lili", "female female"), t= (t+1)%2;}}} Class Output implements Runnable{private res R;output (res r) {this.r=r;} public void Run () {while (true) {synchronized (R) {r.out ();}}}} Class Inputoutputdemo4{public static void Main (string[] args) {res r=new res (); New Thread (New Input (R)). Start (); new Thread (new Output (R)). Start ();/*input in=new Input (r); output Out=new output (r); Thread T1=new thread (in); Thread T2=new thread (out); T1.start (); T2.start (); */}} 



The waiting wake mechanism of communication between multiple threads

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.