Difference between wait() and sleep()

前端 未结 30 3157
無奈伤痛
無奈伤痛 2020-11-22 00:24

What is the difference between a wait() and sleep() in Threads?

Is my understanding that a wait()-ing Thread is still in runni

相关标签:
30条回答
  • 2020-11-22 00:48

    One potential big difference between sleep/interrupt and wait/notify is that

    • calling interrupt() during sleep() always throws an exception (e.g. InterruptedException), whereas
    • calling notify() during wait() does not.

    Generating an exception when not needed is inefficient. If you have threads communicating with each other at a high rate, then it would be generating a lot of exceptions if you were calling interrupt all the time, which is a total waste of CPU.

    0 讨论(0)
  • 2020-11-22 00:51

    wait releases the lock and sleep doesn't. A thread in waiting state is eligible for waking up as soon as notify or notifyAll is called. But in case of sleep the thread keeps the lock and it'll only be eligible once the sleep time is over.

    0 讨论(0)
  • 2020-11-22 00:52

    Difference between wait() and sleep()

    • The fundamental difference is that wait() is from Object and sleep() is a static method of Thread.

    • The major difference is that wait() releases the lock while sleep() doesn’t release any lock while waiting.

    • wait() is used for inter-thread communication while sleep() is used to introduce a pause on execution, generally.

    • wait() should be called from inside synchronise or else we get an IllegalMonitorStateException, while sleep() can be called anywhere.

    • To start a thread again from wait(), you have to call notify() or notifyAll(). As for sleep(), the thread gets started after a specified time interval.

    Similarities

    • Both make the current thread go into the Not Runnable state.
    • Both are native methods.
    0 讨论(0)
  • 2020-11-22 00:52

    From oracle documentation page on wait() method of Object:

    public final void wait()
    
    1. Causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).
    2. The current thread must own this object's monitor. The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up
    3. interrupts and spurious wakeups are possible
    4. This method should only be called by a thread that is the owner of this object's monitor

    This method throws

    1. IllegalMonitorStateException - if the current thread is not the owner of the object's monitor.

    2. InterruptedException - if any thread interrupted the current thread before or while the current thread was waiting for a notification. The interrupted status of the current thread is cleared when this exception is thrown.

    From oracle documentation page on sleep() method of Thread class:

    public static void sleep(long millis)
    
    1. Causes the currently executing thread to sleep (temporarily cease execution) for the specified number of milliseconds, subject to the precision and accuracy of system timers and schedulers.
    2. The thread does not lose ownership of any monitors.

    This method throws:

    1. IllegalArgumentException - if the value of millis is negative

    2. InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.

    Other key difference:

    wait() is a non-static method (instance method) unlike static method sleep() (class method).

    0 讨论(0)
  • 2020-11-22 00:53

    This is a very simple question, because both these methods have a totally different use.

    The major difference is to wait to release the lock or monitor while sleep doesn't release any lock or monitor while waiting. Wait is used for inter-thread communication while sleep is used to introduce pause on execution.

    This was just a clear and basic explanation, if you want more than that then continue reading.

    In case of wait() method thread goes in waiting state and it won't come back automatically until we call the notify() method (or notifyAll() if you have more then one thread in waiting state and you want to wake all of those thread). And you need synchronized or object lock or class lock to access the wait() or notify() or notifyAll() methods. And one more thing, the wait() method is used for inter-thread communication because if a thread goes in waiting state you'll need another thread to wake that thread.

    But in case of sleep() this is a method which is used to hold the process for few seconds or the time you wanted. Because you don't need to provoke any notify() or notifyAll() method to get that thread back. Or you don't need any other thread to call back that thread. Like if you want something should happen after few seconds like in a game after user's turn you want the user to wait until the computer plays then you can mention the sleep() method.

    And one more important difference which is asked often in interviews: sleep() belongs to Thread class and wait() belongs to Object class.

    These are all the differences between sleep() and wait().

    And there is a similarity between both methods: they both are checked statement so you need try catch or throws to access these methods.

    I hope this will help you.

    0 讨论(0)
  • 2020-11-22 00:53

    In my opinion, the main difference between both mechanisms is that sleep/interrupt is the most basic way of handling threads, whereas wait/notify is an abstraction aimed to do thread inter-communication easier. This means that sleep/interrupt can do anything, but that this specific task is harder to do.

    Why is wait/notify more suitable? Here are some personal considerations:

    1. It enforces centralization. It allows to coordinate the communication between a group of threads with a single shared object. This simplifies the work a lot.

    2. It enforces synchronization. Because it makes the programmer wrap the call to wait/notify in a synchronized block.

    3. It's independent of the thread origin and number. With this approach you can add more threads arbitrarily without editing the other threads or keeping a track of the existing ones. If you used sleep/interrupt, first you would need to keep the references to the sleeping threads, and then interrupt them one by one, by hand.

    An example from the real life that is good to explain this is a classic restaurant and the method that the personnel use to communicate among them: The waiters leave the customer requests in a central place (a cork board, a table, etc.), ring a bell, and the workers from the kitchen come to take such requests. Once that there is any course ready, the kitchen personnel ring the bell again so that the waiters are aware and take them to the customers.

    0 讨论(0)
提交回复
热议问题