Home » Java programming language

Differences between wait() and sleep() methods in Java

wait() vs sleep() methods in Java: Here, we are going to learn what are the differences between wait() and sleep() methods in Java programming language?
Submitted by Preeti Jain, on August 11, 2019

wait() and sleep() methods in Java

First, we will see how wait() method differs from sleep() method in Java?

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.
  • 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,
    1. wait()
      This method causes the current thread to wait until another thread get notification by calling notify()or notifyAll() method of the object.
    2. 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.
    3. 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.
  •   

Second, we will see how sleep() method differs from the wait() method in Java?

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.
  • 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,
    1. 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.
    2. 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.





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.