1. 程式人生 > >執行緒中sleep方法和wait方法有什麼區別?(轉) 執行緒中sleep方法和wait方法有什麼區別?

執行緒中sleep方法和wait方法有什麼區別?(轉) 執行緒中sleep方法和wait方法有什麼區別?

本文轉自https://www.cnblogs.com/linkstar/p/6043846.html

執行緒中sleep方法和wait方法有什麼區別?

 

如果你沒有接觸過java的多執行緒,那麼多對於這兩個方法可能有點陌生,看名字好像這兩個方法是差不多的,但是實際上面差別好大。

 

首先我們看一下官方的API

 

Sleep(sleep有兩個方法,另一個方法傳遞兩個引數,還有一個引數也是時間,只不過是納秒級別的,所以和這個方法幾乎一樣,所以這裡只分析這個毫秒級別的方法)

public static void sleep(long millis) throws InterruptedException

首先是個靜態的方法,入參是一個毫秒數,會丟擲InterruptedException異常

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

讓當前執行的執行緒睡眠(臨時停止執行)一段毫秒數的時間,這個時間的精確性受到系統計時器和程式排程精度影響。這個執行緒不會失去任何監視器的所有權。

 

wait(也有多個方法,就不一一介紹了)

public final void wait()throws InterruptedException

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

讓當前程序等待直到另一個其他的執行緒代理呼叫了notify() 方法或者notifyAll() 方法之後。換句話說,這個方法的行為和wait(0)方法是類似的。

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

當前執行緒一定要有自己物件的監視器。這個執行緒釋放了自己的監視器然後等待直到其他執行緒換線這個等待執行緒的監視器,通過notify() 方法或者notifyAll() 方法。然後該執行緒將等到重新獲得對監視器的所有權後才能繼續執行。

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }
 

 

後面對這兩個方法做個總結

 

不同點 : 
1.每個物件都有一個鎖來控制同步訪問。Synchronized關鍵字可以和物件的鎖互動,來實現執行緒的同步。  
sleep方法沒有釋放鎖,而wait方法釋放了鎖,使得其他執行緒可以使用同步控制塊或者方法。  
2.wait,notify和notifyAll只能在同步控制方法或者同步控制塊裡面使用,而sleep可以在任何地方使用  
3.sleep必須捕獲異常,而wait,notify和notifyAll不需要捕獲異常 

4.sleep是執行緒類(Thread)的方法,導致此執行緒暫停執行指定時間,給執行機會給其他執行緒,但是監控狀態依然保持,到時後會自動恢復。呼叫sleep不會釋放物件鎖。

5.wait是Object類的方法,對此物件呼叫wait方法導致本執行緒放棄物件鎖,進入等待此物件的等待鎖定池,只有針對此物件發出notify方法(或notifyAll)後本執行緒才進入物件鎖定池準備獲得物件鎖進入執行狀態。

如果你沒有接觸過java的多執行緒,那麼多對於這兩個方法可能有點陌生,看名字好像這兩個方法是差不多的,但是實際上面差別好大。

 

首先我們看一下官方的API

 

Sleep(sleep有兩個方法,另一個方法傳遞兩個引數,還有一個引數也是時間,只不過是納秒級別的,所以和這個方法幾乎一樣,所以這裡只分析這個毫秒級別的方法)

public static void sleep(long millis) throws InterruptedException

首先是個靜態的方法,入參是一個毫秒數,會丟擲InterruptedException異常

Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers. The thread does not lose ownership of any monitors.

讓當前執行的執行緒睡眠(臨時停止執行)一段毫秒數的時間,這個時間的精確性受到系統計時器和程式排程精度影響。這個執行緒不會失去任何監視器的所有權。

 

wait(也有多個方法,就不一一介紹了)

public final void wait()throws InterruptedException

Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

讓當前程序等待直到另一個其他的執行緒代理呼叫了notify() 方法或者notifyAll() 方法之後。換句話說,這個方法的行為和wait(0)方法是類似的。

The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

當前執行緒一定要有自己物件的監視器。這個執行緒釋放了自己的監視器然後等待直到其他執行緒換線這個等待執行緒的監視器,通過notify() 方法或者notifyAll() 方法。然後該執行緒將等到重新獲得對監視器的所有權後才能繼續執行。

As in the one argument version, interrupts and spurious wakeups are possible, and this method should always be used in a loop:

     synchronized (obj) {
         while (<condition does not hold>)
             obj.wait();
         ... // Perform action appropriate to condition
     }
 

 

後面對這兩個方法做個總結