Java wait notify
2019獨角獸企業重金招聘Python工程師標準>>>
Java wait && notify
?wait、notify和notifyAll方法是Object類的final native方法,所以這些方法不能被子類重寫。
方法 notifyAll()
Wakes up all threads that are waiting on this object's monitor.?A?thread waits on an object's monitor by calling one of the{wait} methods.
該方法只能在同步方法或同步塊內部調用。如果當前線程不是鎖的持有者,該方法拋出一個IllegalMonitorStateException異常。
?
方法 notify()
Wakes up a single thread that is waiting on this object's?monitor. If any threads are waiting on this object, one of them?is chosen to be awakened. The choice is arbitrary and occurs at?the discretion of the implementation. A thread waits on an object's?monitor by calling one of the {wait} methods.
該方法只能在同步方法或同步塊內部調用。如果當前線程不是鎖的持有者,該方法拋出一個IllegalMonitorStateException異常。
?
方法 wait()
Causes the current thread to wait until another thread invokes the?{java.lang.Object#notify()} method or the?{java.lang.Object#notifyAll()} method for this object.The current thread must own this object's monitor.?
調用該方法的線程進入WAITING 狀態,只有等待另外線程的通知或被中斷才會返回,需要注意的是調用wait方法后,才會釋放對象的鎖。
該方法只能在同步方法中調用。如果當前線程不是鎖的持有者,該方法拋出一個IllegalMonitorStateException異常。下面是一個wait的示例
synchronized?(object)?{while?(<condition?does?not?hold>)object.wait();// ...? }?
方法 wait(long millis)&&wait(long millis,int nanos)
Causes the current thread to wait until another thread invokes the?{?java.lang.Object#notify()} method or the?{java.lang.Object#notifyAll()} method for this object, or?some other thread interrupts the current thread, or a certain?amount of real time has elapsed(消逝,過去).
超時等待一段時間后,這里的參數時間是毫秒,也就是等待長達n毫秒,如果沒有通知就超時返回。
這些方法只能在同步方法中調用。如果當前線程不是鎖的持有者,該方法拋出一個IllegalMonitorStateException異常。
timeout -- 最大的等待時間(以毫秒為單位)。
nanos?? -- 額外的時間,在納秒范圍為0-999999。
?Object.wait()和Object.notify()和Object.notifyAll()必須寫在synchronized方法內部或者synchronized塊內部,這是因為:這幾個方法要求當前正在運行object.wait()方法的線程擁有object的對象鎖(內置鎖)。即使你確實知道當前上下文線程確實擁有了對象鎖,也不能將object.wait()這樣的語句寫在當前上下文中。?
?
下面這段代碼的寫法是錯誤的。
package?sync;class?A?{public?synchronized?void?printThreadInfo()?throws?InterruptedException?{Thread?t?=?Thread.currentThread();System.out.println("ThreadID:"?+?t.getId()?+?",?ThreadName:"?+?t.getName());} }public?class?ObjectWaitTest?{public?static?void?main(String?args[])?{A?a?=?new?A();//因為printThreadInfo()方法拋出InterruptedException異常,所以這里必須使用try-catch塊try?{a.printThreadInfo();a.wait();}?catch?(InterruptedException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}} }應該要這么寫:
package?sync;class?A?{public?synchronized?void?printThreadInfo()?throws?InterruptedException?{Thread?t?=?Thread.currentThread();System.out.println("ThreadID:"?+?t.getId()?+?",?ThreadName:"?+?t.getName());//?this.wait();//一直等待this.wait(1000);//等待1000ms//?super.wait(1000);} }public?class?ObjectWaitTest?{public?static?void?main(String?args[])?{A?a?=?new?A();//因為printThreadInfo()方法拋出InterruptedException異常,所以這里必須使用try-catch塊try?{a.printThreadInfo();}?catch?(InterruptedException?e)?{//?TODO?Auto-generated?catch?blocke.printStackTrace();}Thread?t?=?Thread.currentThread();System.out.println("ThreadID:"?+?t.getId()?+?",?ThreadName:"?+?t.getName());} }完整示例,
import java.text.SimpleDateFormat; import java.util.Date; import java.util.concurrent.TimeUnit;public class WaitNotifyDemo {static boolean flag = true;static Object lock = new Object();public static void main(String[] args) throws InterruptedException {Thread waitThread = new Thread(new Wait(), "waitThread");waitThread.start();TimeUnit.SECONDS.sleep(1);Thread notifyThread = new Thread(new Notify(), "notifyThread");notifyThread.start();}static class Wait implements Runnable {@Overridepublic void run() {// 加鎖 擁有lock 的 monitorsynchronized (lock) {while (flag) {try {System.out.println(Thread.currentThread() + " flag is true. wait @ " +new SimpleDateFormat("HH:mm:ss").format(new Date()));// 釋放了對象的監視器鎖lock.wait();} catch (InterruptedException e) {e.printStackTrace();}}System.out.println(Thread.currentThread() + " flag is false. running @ " +new SimpleDateFormat("HH:mm:ss").format(new Date()));}}}static class Notify implements Runnable {@Overridepublic void run() {synchronized (lock) {System.out.println(Thread.currentThread() + " hold lock. notify @ " +new SimpleDateFormat("HH:mm:ss").format(new Date()));lock.notifyAll();flag = false;try {Thread.sleep(1000 * 5);} catch (InterruptedException e) {e.printStackTrace();}}}} }以上就是關于wait和notify方法的用法。
參考:http://www.cnblogs.com/xwdreamer/archive/2012/05/12/2496843.html
后記:Thread.sleep()與Object.wait()二者都可以暫停當前線程,釋放CPU控制權,主要的區別在于Object.wait()在釋放CPU同時,釋放了對象鎖的控制。
==============END==============
轉載于:https://my.oschina.net/xinxingegeya/blog/345816
總結
以上是生活随笔為你收集整理的Java wait notify的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 本地环境和测试环境搭建
- 下一篇: Java 操作POI 之复制sheet页