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