Object.wait理解

Object.wait理解

wait注釋:
public final void wait(long timeout)
                throws InterruptedException
Causes the current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed.
The current thread must own this object's monitor.

This method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes and lies dormant until one of four things happens:

Some other thread invokes the notify method for this object and thread T happens to be arbitrarily chosen as the thread to be awakened.
Some other thread invokes the notifyAll method for this object.
Some other thread interrupts thread T.
The specified amount of real time has elapsed, more or less. If timeout is zero, however, then real time is not taken into consideration and the thread simply waits until notified.
The thread T is then removed from the wait set for this object and re-enabled for thread scheduling. It then competes in the usual manner with other threads for the right to synchronize on the object; once it has gained control of the object, all its synchronization claims on the object are restored to the status quo ante - that is, to the situation as of the time that the wait method was invoked. Thread T then returns from the invocation of the wait method. Thus, on return from the wait method, the synchronization state of the object and of thread T is exactly as it was when the wait method was invoked.
A thread can also wake up without being notified, interrupted, or timing out, a so-called spurious wakeup. While this will rarely occur in practice, applications must guard against it by testing for the condition that should have caused the thread to be awakened, and continuing to wait if the condition is not satisfied. In other words, waits should always occur in loops, like this one:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait(timeout);
         ... // Perform action appropriate to condition
     }
 
(For more information on this topic, see Section 3.2.3 in Doug Lea's "Concurrent Programming in Java (Second Edition)" (Addison-Wesley, 2000), or Item 50 in Joshua Bloch's "Effective Java Programming Language Guide" (Addison-Wesley, 2001).
If the current thread is interrupted by any thread before or while it is waiting, then an InterruptedException is thrown. This exception is not thrown until the lock status of this object has been restored as described above.

Note that the wait method, as it places the current thread into the wait set for this object, unlocks only this object; any other objects on which the current thread may be synchronized remain locked while the thread waits.

This method should only be called by a thread that is the owner of this object's monitor. See the notify method for a description of the ways in which a thread can become the owner of a monitor.

Parameters:
timeout - the maximum time to wait in milliseconds.
Throws:
IllegalArgumentException - if the value of timeout is negative.
IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.
InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

有以上注釋可以總結(jié)出

  • 當(dāng)一個線程執(zhí)行一個Objectwait方法后炕置,只有此object調(diào)用notify或者notifyAll方法喚醒。
  • 當(dāng)前線程必須只有此Object的鎖(monitor)椰拒,所以wait方法必須在同步塊中莫其。
  • 當(dāng)前線程執(zhí)行一個Objectwait方法后,當(dāng)前線程將不在持有此Object的鎖(monitor)耸三。
  • 喚醒wait有四種方式:
    • 調(diào)用notify方法
    • 調(diào)用notifyAll方法
    • 通過線程阻斷(interrupts)
    • 設(shè)置wait方法的喚醒時間
  • wait線程乱陡,可能出現(xiàn)“假喚醒”(spurious wakeup),應(yīng)該加條件控制仪壮。
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末憨颠,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子积锅,更是在濱河造成了極大的恐慌爽彤,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件缚陷,死亡現(xiàn)場離奇詭異适篙,居然都是意外死亡,警方通過查閱死者的電腦和手機箫爷,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門嚷节,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人虎锚,你說我怎么就攤上這事硫痰。” “怎么了窜护?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵效斑,是天一觀的道長。 經(jīng)常有香客問我柱徙,道長缓屠,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任护侮,我火速辦了婚禮敌完,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘概行。我一直安慰自己蠢挡,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布禽炬。 她就那樣靜靜地躺著,像睡著了一般腹尖。 火紅的嫁衣襯著肌膚如雪伐脖。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天绎巨,我揣著相機與錄音蠕啄,去河邊找鬼。 笑死和媳,一個胖子當(dāng)著我的面吹牛哈街,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播骚秦,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼骤竹,長吁一口氣:“原來是場噩夢啊……” “哼往毡!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起懒震,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤嗤详,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后递宅,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體办龄,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年安接,在試婚紗的時候發(fā)現(xiàn)自己被綠了盏檐。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片驶悟。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖给涕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情够庙,我是刑警寧澤抄邀,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布境肾,位于F島的核電站,受9級特大地震影響奥喻,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜纯趋,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一吵冒、第九天 我趴在偏房一處隱蔽的房頂上張望西剥。 院中可真熱鬧,春花似錦揪阿、人聲如沸疗我。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至矿瘦,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間潮秘,已是汗流浹背易结。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留躏精,地道東北人。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓矗烛,卻偏偏與公主長得像瞭吃,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子歪架,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容