Difference between sleep(), suspend() and wait() 8

The question that was popped-up to us was what is the difference between suspend(), wait() and sleep() (methods of Java)?

Though minute, there is a subtle difference among the three. Here’s how:

sleep() is a static method that is used to send the calling thread into a non-runnable state for the given duration of time. The important part for this is recognizing the “calling thread”, which is actually the thread in which the sleep() method is invoked rather than the thread object which may (which is essentially a violation of Java standards) invoke this method. What this means is, while calling Thread.sleep(<duration in milliseconds>) is appropriate and will send the invoking thread to non-runnable state, but calling t.sleep(<duration in milliseconds>) is inappropriate as it would cause the thread in which this call appears to go to non-runnable state instead of the “t” thread whose object is invoking the method. While in “sleep”, the thread will keep all the monitor locks which it might be holding at the time of sleep invocation.

The t.suspend() method has been deprecated, as it is inherently deadlock-prone.It suspends the thread on which it is invoked. If the target thread holds a lock on the monitor protecting a critical system resource when it is suspended, no thread can access this resource until the target thread is resumed. If the thread that would resume the target thread attempts to lock this monitor prior to calling resume, deadlock results.

t.wait() Causes current thread to wait until either another thread invokes the notify() method or the notifyAll() method for this object, or a specified amount of time has elapsed. The difference between sleep() and wait() is that sleep() is called on a thread while wait() is called on an object (That’s why it is part of Object class. The parent to all Java classes). The wait() method causes the current thread (call it T) to place itself in the wait set for this object and then to relinquish any and all synchronization claims on this object. Thread T becomes disabled for thread scheduling purposes. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread T.

2.5/5 - (20 votes)

8 thoughts on “Difference between sleep(), suspend() and wait()

  1. Reply Sandeep Sep 9,2013 11:56 pm

    I don’t know why its confusing that wait and sleep methods have to be invoked from a synchronized block of code. You explained it well. Another useful article which I came across while searching was wait and sleep difference

  2. Reply amino prime Free Trial Dec 14,2013 1:38 am

    Hello! Quick question that’s entirely off topic.
    Do you know how to make your site mobile friendly?
    My website looks weird when browsing from my iphone4.
    I’m trying to find a template or plugin that might
    be able to fix this issue. If you have any recommendations, please share.
    Appreciate it!

  3. Reply Margarette Jun 22,2014 10:48 pm

    hi!,I love your writing so so much! proportion we keep up a correspondence more about your article on AOL?
    I require a specialist on this space to solve my problem.

    May be that is you! Taking a look ahead to peer you.

  4. Reply Janeen Aug 16,2014 12:39 am

    I really like your blog.. very nice colors & theme.
    Did you design this website yourself or did
    you hire someone to do it for you? Plz answer back as I’m looking to design my own blog and would like to find
    out where u got this from. cheers

    • Reply Anupam Jain Aug 29,2014 12:40 pm

      Could you please contact us on our personal mail-ids for more information regarding this. IDs are available on About Us page.
      Thanks 🙂

  5. Reply cialis france Dec 25,2014 2:28 am

    I was able to find good info from your content.|
    Thanks for another fantastic post. The place else may just anyone get that kind of info in such a perfect means of writing? I have a presentation subsequent week, and I am at the look for such information.

  6. Reply Mixcib Jul 19,2020 1:48 pm

    Thank you very much for the invitation :). Best wishes.
    PS: How are you? I am from France 🙂

  7. Reply Kenneth Jan 26,2022 1:48 pm

    It’s awesome in support of me to have a web site, which is beneficial for my experience.
    thanks admin

Leave a Reply to Sandeep Cancel Reply