Java - Thread wait() and sleep() Methods

By Preeti Jain Last updated : January 26, 2024

wait() Method

  • This method is available in java.lang package.
  • This method is used to pause a Thread in Java.
  • This method is defined in Object class.
  • 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.
  • 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.
  • 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)
  • This method will be called from synchronized context only (i.e. we can call this method from either synchronize method or synchronized block).
  • In case of wait() method, Waiting for the thread will wait until a condition is true it is based on condition.

Syntax

final void wait(){}
final void wait(long ms, int ns){}
final void wait(long ms){}

Overloaded Methods

  • wait()
    This method causes the current thread to wait until another thread get notification by calling notify()or notifyAll() method of the object.
  • wait(long ms)
    This method causes the current thread to wait for a specified amount of time until another thread notification by calling notify() or notifyAll() method of the object.
  • wait(long ms, int ns)
    This method causes the current thread to wait for a specified amount of time in milliseconds and nanoseconds until another thread notification by calling notify() or notifyAll() method of the object.

We should go for wait() method if we want to wait for a certain condition.

sleep() Method

  • This method is available in java.lang package.
  • This method is used to pause a thread for a short duration in Java.
  • This method is defined in Thread class.
  • 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.
  • 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).
  • This method is useful for when a thread wants to wait or sleep for a short duration.
  • 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).
  • 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.

Syntax

static void sleep(long ms){}
static void sleep(long ms, int ns){}

Overloaded Methods

  • sleep(long ms)
    This method is applicable when we want to stop current executing thread for a particular amount of time in milliseconds or other words if a thread causes the current thread to stop executing for some time in milliseconds given in the method.
  • sleep(long ms, int ns)
    This method is applicable when we want to stop current executing thread for particular amount of time in milliseconds + nanoseconds (i.e. with additional time in nanoseconds) as well or in other words if a thread causes current thread to stop executing for some time in milliseconds + nanoseconds given in the method.

We should go for sleep() method if we want to wait for a certain duration.

wait() Vs sleep()

The wait() method is used to wait for a certain condition whereas, the sleep() method is used to wait for a certain duration.


Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.