4.等待和通知
阿新 • • 發佈:2019-01-27
interrupt new 一個 ted sta finish 關鍵字 oid 實例
等待和通知
Object對象提供了wait( ) 方法和 notify ( ) 方法。
wait( )方法需要配對著synchronized 關鍵字使用。
當一個線程的資源調用 wait( )方法時,它會失去鎖,然後進入等待序列。
當其他線程調用notify方法時才會重新激活。
實例:
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(); } }
運行結果:
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
notify( )方法只會隨機激活等待序列中的一個線程,而notifyAll( )方法激活所有等待的線程。
實例:
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(); } }
運行結果:
WaitThread Start!
Object wait!!!
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
結果分析:
兩個WaitThread線程只被喚醒了一個。
實例:
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(); } }
運行結果:
WaitThread Start!
Object wait!!!
WaitThread Start!
Object wait!!!
NotifyThread Start!
Object notify
NotifyThread Finish!
WaitThread Finish!
WaitThread Finish!
結果分析:
notifyAll( )方法會喚醒處在等待序列中的所有線程。
4.等待和通知