Inter-thread communication:
In fact, multiple threads are manipulating the same resource.
But the action action is different
Example:
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 ();}}
Although the security problem solved, but did not appear we want a male and a female alternate scene
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 ();}}
Wait
Notify ();
Notifyall ();
are used in synchronization because you want to hold the monitor (lock) operation.
So to use in synchronization, because only synchronization has a lock.
Why do these operations threads of Phoenix fly to define the object class?
Because these methods must identify the locks in the thread they are operating on when they are operating synchronously,
Only the same locked waiting thread 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 any object, so it can be defined in the object class by a method called by any object.
The following code improvements:
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 (); */}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The waiting wake mechanism of communication between multiple threads