Java中的wait()和sleep()方法 (wait() and sleep() methods in Java)
First, we will see how wait() method differs from sleep() method in Java?
首先,我們將看到wait()方法與Java中的sleep()方法有何不同?
wait()方法 (wait() Method)
This method is available in java.lang package.
此方法在java.lang包中可用。
This method is used to pause a Thread in Java.
此方法用于暫停Java中的線程。
This method is defined in Object class.
此方法在Object類中定義。
This method releases the monitor or acquired the lock on that monitor while waiting.
此方法在等待時釋放監視器或獲得該監視器的鎖定。
wait() is a non-static method (i.e. instance method) so this method is accessible with the help of object class.
wait()是一個非靜態方法(即實例方法),因此可以在對象類的幫助下訪問此方法。
Let suppose if our thread is waiting for execution so it wakes up at one condition is that when other calls notify() or notifyAll() method on the same object.
讓我們假設,如果我們的線程正在等待執行,那么它在一種情況下被喚醒的原因是,當其他線程在同一對象上調用notify()或notifyAll()方法時。
This method is useful for inter-thread communication.
此方法對于線程間通信很有用。
In case of wait() method, waiting thread does not go into Runnable state directly (i.e. If waiting thread wake up then it first acquired the lock then after goes into Runnable state)
在使用wait()方法的情況下,等待線程不會直接進入Runnable狀態(即,如果等待線程喚醒,則它首先獲取了鎖,然后進入Runnable狀態)
This method will be called from synchronized context only (i.e. we can call this method from either synchronize method or synchronized block).
僅從同步上下文調用此方法(即,我們可以從syncize方法或synced塊調用此方法)。
In case of wait() method, Waiting for the thread will wait until a condition is true it is based on condition.
如果使用wait()方法,則等待線程將一直等到條件為真(基于條件)。
The syntax of this method is given below :
該方法的語法如下:
final void wait(){} final void wait(long ms, int ns){} final void wait(long ms){}
This method is an overloaded method so we will see all the variations given below,
此方法是重載方法,因此我們將看到下面給出的所有變化,
- wait()等待()
- wait(long ms)等待(長毫秒)
- wait(long ms, int ns)等待(長毫秒,整數ns)
We should go for wait() method if we want to wait for a certain condition.
如果我們要等待某個條件,就應該使用wait()方法。
Second, we will see how sleep() method differs from the wait() method in Java?
其次,我們將看到sleep()方法與Java中的wait()方法有何不同?
sleep()方法 (sleep() Method)
This method is available in java.lang package.
此方法在java.lang包中可用。
This method is used to pause a thread for a short duration in Java.
此方法用于在Java中將線程暫停一小段時間。
This method is defined in Thread class.
此方法在Thread類中定義。
This method does not release the monitor or acquired lock on that object while a thread is waiting.
在線程等待時,此方法不會釋放監視器或對該對象的獲取鎖定。
sleep() is a static method (i.e. class method) so this method is accessible with Classname.
sleep()是靜態方法(即類方法),因此可以使用Classname訪問此方法。
Let suppose if our thread is waiting for execution so it does not wake up at condition (i.e. we don't need to call notify() or notifyAll() method to wake up).
假設我們的線程正在等待執行,因此它不會在條件下喚醒(即,我們不需要調用notify()或notifyAll()方法來喚醒)。
This method is useful for when a thread wants to wait or sleep for a short duration.
當線程想要短時間等待或Hibernate時,此方法很有用。
In case of sleep() method sleeping thread goes into Runnable state directly (i.e. If sleeping thread wakes up then it does not need to acquire the lock).
如果使用sleep()方法,則睡眠線程直接進入Runnable狀態(即,如果睡眠線程喚醒,則不需要獲取鎖)。
This method will be called from non-synchronize context (i.e. we can call this method from non-synchronize method or block).
該方法將從非同步上下文中調用(即,我們可以從非同步方法或塊中調用此方法)。
In the case of sleep() method, the sleeping thread will wait for a particular duration.
對于sleep()方法,睡眠線程將等待特定的持續時間。
The syntax of this method is given below:
該方法的語法如下:
static void sleep(long ms){} static void sleep(long ms, int ns){}
This method is an overloaded method so we will see all the variations given below,
此方法是重載方法,因此我們將看到下面給出的所有變化,
- sleep(long ms)睡眠(長毫秒)
- sleep(long ms, int ns)睡眠(長毫秒,整數ns)
We should go for sleep() method if we want to wait for a certain duration.
如果我們要等待一定的時間,我們應該使用sleep()方法。
翻譯自: https://www.includehelp.com/java/differences-between-wait-and-sleep-methods-in-java.aspx