The Object object Object provides the wait() method and the notify () method.
The wait() method needs to be used with the synchronized keyword.
When a thread's resource calls the wait() method, it loses the lock and enters the wait sequence.
It is only reactivated when another thread calls the notify method.
Example:
package com.xm.thread.t_19_01_27; import java.util.concurrent.TimeUnit; public class NotifyAndWait{ private static Object object = new Object(); static class WaitThread extends Thread{ @Override public void run() { System.out.println("WaitThread Start!"); try { System.out.println("Object wait!!!"); synchronized (object) { object.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("WaitThread Finish!"); } } static class NotifyThread extends Thread{ @Override public void run() { System.out.println("NotifyThread Start!"); System.out.println("Object notify"); synchronized (object) { object.notify(); } System.out.println("NotifyThread Finish!"); } } public static void main(String[] args) throws InterruptedException { new WaitThread().start(); TimeUnit.SECONDS.sleep(5); new NotifyThread().start(); } }
Operation result:
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
The notify() method only randomly activates one thread in the wait sequence, while the notifyAll() method activates all waiting threads.
Example:
package com.xm.thread.t_19_01_27; import java.util.concurrent.TimeUnit; public class NotifyAndWait{ private static Object object = new Object(); static class WaitThread extends Thread{ @Override public void run() { System.out.println("WaitThread Start!"); try { System.out.println("Object wait!!!"); synchronized (object) { object.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("WaitThread Finish!"); } } static class NotifyThread extends Thread{ @Override public void run() { System.out.println("NotifyThread Start!"); System.out.println("Object notify"); synchronized (object) { object.notify(); } System.out.println("NotifyThread Finish!"); } } public static void main(String[] args) throws InterruptedException { new WaitThread().start(); new WaitThread().start(); TimeUnit.SECONDS.sleep(5); new NotifyThread().start(); } }
Operation result:
WaitThread Start!
Object wait!!!
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
Results analysis:
Only one of the two WaitThread threads was woken up.
Example:
package com.xm.thread.t_19_01_27; import java.util.concurrent.TimeUnit; public class NotifyAndWait{ private static Object object = new Object(); static class WaitThread extends Thread{ @Override public void run() { System.out.println("WaitThread Start!"); try { System.out.println("Object wait!!!"); synchronized (object) { object.wait(); } } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("WaitThread Finish!"); } } static class NotifyThread extends Thread{ @Override public void run() { System.out.println("NotifyThread Start!"); System.out.println("Object notify"); synchronized (object) { object.notifyAll(); } System.out.println("NotifyThread Finish!"); } } public static void main(String[] args) throws InterruptedException { new WaitThread().start(); new WaitThread().start(); TimeUnit.SECONDS.sleep(5); new NotifyThread().start(); } }
Operation result:
WaitThread Start!
Object wait!!!
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
WaitThread Finish!
Results analysis:
The notifyAll() method wakes up all threads in the waiting sequence.