1. Wait, notify overview

  • The goal is thread communication
  • Both wait and notify need to be placed in synchronized(both methods and blocks of code).
  • Synchronized is released after encountering “Wait”.
  • Notify wakes up one thread, and notifyAll wakes up multiple threads (one by one).
  • You can view the wait threads via Jconsole

Example 2.

2.1 Implement thread communication without wait and Notify

*/ public class SignalCommunication {private volatile int */ public class SignalCommunication {private volatile int */ public class SignalCommunication {private volatile int */  signal; public void set (int value) { this.signal = value; } public int get () { return signal; } public static void main(String[] args) { SignalCommunication d = new SignalCommunication(); New Thread(new Runnable() {@override public void run() {system.out.println () {Thread(new Runnable() {@override public void run() {system.out.println () ); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } d.set(1); System.out.println(" Status changed successfully... ") ); } }).start(); New Thread(new Runnable() {@override public void run() {// While (d.set ()! = 1) { try { Thread.sleep(1500); } catch (InterruptedException e) { e.printStackTrace(); } // When the signal is 1, execute system.out.println (" simulate code execution...") ); } }).start(); }}Copy the code

2.2 Implement Thread Communication through Wait and Notify

  • Anonymous new Threads then communicate with multithreaded threads (wait,notifyl)
/** Both wait and notify must be synchronized. Synchronized is released after encountering "Wait". Otherwise, the thread waits. Notify or NotifyAll locks one. */ public class WaitNotifyCommunication {private volatile int signal; public void set (int value) { this.signal = value; } public int get () { return signal; } public static void main(String[] args) { WaitNotifyCommunication waitNotifyCommunication = new WaitNotifyCommunication(); New Thread(new Runnable() {@override public void run() {public void run() {public void run() { Synchronized (waitNotifyCommunication) {system.out.println (" The thread that changes the state executes..." ); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } waitNotifyCommunication.set(1); System.out.println(" Status changed successfully... ") ); waitNotifyCommunication.notify(); //notify(); }}}).start();}}}).start(); New Thread(new Runnable() {@override public void run() {synchronized (waitNotifyCommunication) { Otherwise cannot perform the while (waitNotifyCommunication. The get ()! = 1) { try { waitNotifyCommunication.wait(); } catch (InterruptedException e) { e.printStackTrace(); } // When the signal is 1, execute system.out.println (" simulate code execution...") ); } } }).start(); }}Copy the code

2.3 NotifyAll Implements thread communication through WAIT

  • Implementation of multithreaded thread communication (wait,notifyall)
  • Thread1 thread 1, Thread2 thread 2, WaitNotifyAllCommunication thread communication, execute the main function
public class Thread1 implements Runnable { private WaitNotifyAllCommunication waitNotifyAllCommunication; public Thread1(WaitNotifyAllCommunication waitNotifyAllCommunication) { this.waitNotifyAllCommunication = waitNotifyAllCommunication; } @Override public void run() { waitNotifyAllCommunication.set(); }}Copy the code
public class Thread2 implements Runnable { private WaitNotifyAllCommunication waitNotifyAllCommunication; public Thread2(WaitNotifyAllCommunication waitNotifyAllCommunication) { this.waitNotifyAllCommunication = waitNotifyAllCommunication; } @Override public void run() { waitNotifyAllCommunication.get(); }}Copy the code
/** Both Waitall and NotifyAll need to be placed in synchronized. Synchronized is released after encountering "Wait". Otherwise, the thread waits. Notify or notifyAll locks * 2. Adopted here is that the implementation of the classes and multithreading example of communication thread * / public class WaitNotifyAllCommunication {private volatile int signal; public synchronized void set () { signal = 1; // Notify randomly wakes up a thread in wait state notifyAll(); System.out.println(" notifyAll ") // notifyAll (" notifyAll ") ); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); }} public synchronized int get () {system.out.println (thread.currentThread ().getName() + ); if(signal ! = 1) { try { wait(); System.out.println(" wake up "); } catch (InterruptedException e) { e.printStackTrace(); }} system.out.println (thread.currentThread ().getName()) + ); return signal; } /** /** / The thread-2 method executes... The thread-0 method executes... The thread-3 method executes... Wake up thread after wake up sleep begins... Thread-3 method executed after wake up... After wake up thread-0 method is executed... Thread-2 method executed after wake up... After wake up thread-1 method execution completed... Process finished with exit code 0 */ public static void main(String[] args) { WaitNotifyAllCommunication d = new WaitNotifyAllCommunication(); Thread1 t1 = new Thread1(d); // set Thread2 t2 = new Thread2(d); Thread2: get new Thread(t2).start(); new Thread(t2).start(); new Thread(t2).start(); new Thread(t2).start(); Try {timeunit.seconds.sleep (1); } catch (InterruptedException e) { e.printStackTrace(); } new Thread(t1).start(); }}Copy the code

2.4 Wait, Notify Mode Producer consumer

  • Warehouse1 factory (production/consumption machine)
  • PushTarget producers
  • TakeTarget consumers
  • Main executes the function
public class Warehouse1 { private int count; public final int MAX_COUNT = 10; public synchronized void push () { while(count >= MAX_COUNT) { try { System.out.println(Thread.currentThread().getName() + "The number of stocks reached the upper limit and producers stopped production." ); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count ++; System.out.println(thread.currentThread ().getName()) + "count"; notifyAll(); } public synchronized void take () { while(count <= 0) { try { System.out.println(Thread.currentThread().getName() + " Inventory numbers are zero and consumers are waiting." ); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } count --; System.out.println(thread.currentThread ().getName()) + ""; notifyAll(); }}Copy the code
public class PushTarget implements Runnable { private Producer1 producer1; public PushTarget(Producer1 producer1) { this.producer1 = producer1; } @Override public void run() { while(true) { producer1.push(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}}}Copy the code
public class TakeTarget implements Runnable { private Producer1 producer1; public TakeTarget(Producer1 producer1) { this.producer1 = producer1; } @Override public void run() { while(true) { producer1.take(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); }}}}Copy the code
public class Main { public static void main(String[] args) { Producer1 producer1 = new Producer1(); PushTarget p = new PushTarget(producer1); TakeTarget t = new TakeTarget(producer1); new Thread(p).start(); new Thread(p).start(); new Thread(p).start(); new Thread(p).start(); new Thread(p).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); new Thread(t).start(); }}Copy the code