Wait and notify
Wait and notify can implement communication between threads, and when one thread executes a condition that does not meet the criteria, it can call the wait method to put the thread into a waiting state, and when another thread executes a condition that the waiting thread can execute, call notify can wake the waiting thread. It should be emphasized that the lock must be acquired before calling wait and notify, otherwise the illegalmonitorexception exception will be thrown. The Notify method randomly wakes a thread from the waiting thread to execute, and the Notifyall method wakes up all the waiting threads that compete for time slices. Here is an example of using wait and notify: the read thread is readable when the write thread wakes the signal wake flag to 1 o'clock.
Public class Demo {
private volatile int signal;
public synchronized int getsignal () {
System.out.println (Thread.CurrentThread (). GetName () + "Enter synchronization method");
if (signal! = 1) {
try {
wait ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
return signal;
}
Public synchronized void setsignal (int signal) {
System.out.println (Thread.CurrentThread (). GetName () + "Wake Up");
this.signal = signal;
Notifyall ();
}
Public static void Main (string[] args) {
Demo Demo = new Demo ();
TaskOne TaskOne = new TaskOne (demo);
Tasktwo tasktwo = new Tasktwo (demo);
Turn on Read thread
New Thread (TaskOne). Start ();
New Thread (TaskOne). Start ();
New Thread (TaskOne). Start ();
New Thread (TaskOne). Start ();
Turn on Write threads
New Thread (Tasktwo). Start ();
}
}
public class TaskOne implements Runnable {
Private demo demo;
Public TaskOne (Demo demo) {
This.demo = demo;
@Override
public void Run () {
demo.getsignal ();
public class Tasktwo implements Runnable {
Private demo demo;
Public Tasktwo (Demo demo) {
This.demo = demo;
}
@Override
public void Run () {
Demo.setsignal (1);
}
}
2. Achieving producers and consumers with wait and notify
Public class Table {
private int count;
private static final int max_count = ten;
Public synchronized void push () {
While (Count >= max_count) {
try {
System.out.println (Thread.CurrentThread (). GetName () + "The table is full and the producer stops producing");
wait ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
count++;
System.out.println (Thread.CurrentThread (). GetName () + "Current number of products on table:" + count);
Wake up the consumer with a product on the table
Notifyall ();
}
Public synchronized void Take () {
While (Count <= 0) {
try {
System.out.println (Thread.CurrentThread (). GetName () + "The table is empty, consumers stop spending");
wait ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
count--;
System.out.println (Thread.CurrentThread (). GetName () + "table surplus:" + count);
Wake the producer when the product on the table is consumed
Notifyall ();
}
}
Public class Productor implements Runnable {
private table table;
Public productor (table table) {
this.table = table;
}
@Override
Public void Run () {
While (true) {
Table.push ();
try {
Thread.Sleep (+);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
}
}
Public class Consumer implements Runnable {
private table table;
Public Consumer (table table) {
this.table = table;
}
@Override
Public void Run () {
While (true) {
Table.take ();
try {
Thread.Sleep (+);
} catch (Interruptedexception e) {
e.printstacktrace ();
}
}
}
}
Public class Demo {
Public static void Main (string[] args) {
Table table = new table ();
productor productor = new productor (table);
Consumer Consumer = new Consumer (table);
new Thread (Productor). Start ();
new Thread (Productor). Start ();
new Thread (Productor). Start ();
new Thread (Productor). Start ();
New Thread (consumer). Start ();
New Thread (consumer). Start ();
New Thread (consumer). Start ();
New Thread (consumer). Start ();
New Thread (consumer). Start ();
New Thread (consumer). Start ();
}
}
2.join
The thread that calls the Join method executes before the other thread, and the thread that is being blocked executes after the thread that calls the Join method finishes executing.
public class Joindemo {
Public void A (Thread jointhread) {
System.out.println ("Thread A starts executing ...");
Jointhread.start ();
try {
//When the front-end is blocked until Jointhread executes
Jointhread.join ();
} catch (Interruptedexception e) {
e.printstacktrace ();
}
System.out.println ("Thread A execution ends ...");
}
public void B () {
SYSTEM.OUT.PRINTLN ("The stopper thread starts executing ...");
try {
} catch (Interruptedexception e) {
e.printstacktrace ();
}
SYSTEM.OUT.PRINTLN ("The end of the thread execution ...");
}
public static void main (string[] args) {
Final Joindemo demo = new Joindemo ();
Final thread jointhread = new Thread () {
@Override
public void run () {
&NBS P DEMO.B ();
}
};
New Thread () {
@Override
public void Run () {
DEMO.A (Jointhread);
}
}.start ();
}
}
Resources:
"Java Concurrent programming Combat" dragon Fruit College
Java concurrent Programming principle and Combat 21: Thread Communication Wait¬ify&join