Difference between wait() and sleep()

前端 未结 30 3119
無奈伤痛
無奈伤痛 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:39

    Actually, all this is clearly described in Java docs (but I realized this only after reading the answers).

    http://docs.oracle.com/javase/8/docs/api/index.html :

    wait() - 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 either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.

    sleep() - 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. The thread does not lose ownership of any monitors.

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

    You are correct - Sleep() causes that thread to "sleep" and the CPU will go off and process other threads (otherwise known as context switching) wheras I believe Wait keeps the CPU processing the current thread.

    We have both because although it may seem sensible to let other people use the CPU while you're not using it, actualy there is an overhead to context switching - depending on how long the sleep is for, it can be more expensive in CPU cycles to switch threads than it is to simply have your thread doing nothing for a few ms.

    Also note that sleep forces a context switch.

    Also - in general it's not possible to control context switching - during the Wait the OS may (and will for longer waits) choose to process other threads.

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

    wait() is given inside a synchronized method whereas sleep() is given inside a non-synchronized method because wait() method release the lock on the object but sleep() or yield() does release the lock().

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

    source : http://www.jguru.com/faq/view.jsp?EID=47127

    Thread.sleep() sends the current thread into the "Not Runnable" state for some amount of time. The thread keeps the monitors it has aquired -- i.e. if the thread is currently in a synchronized block or method no other thread can enter this block or method. If another thread calls t.interrupt() it will wake up the sleeping thread.

    Note that sleep is a static method, which means that it always affects the current thread (the one that is executing the sleep method). A common mistake is to call t.sleep() where t is a different thread; even then, it is the current thread that will sleep, not the t thread.

    t.suspend() is deprecated. Using it is possible to halt a thread other than the current thread. A suspended thread keeps all its monitors and since this state is not interruptable it is deadlock prone.

    object.wait() sends the current thread into the "Not Runnable" state, like sleep(), but with a twist. Wait is called on an object, not a thread; we call this object the "lock object." Before lock.wait() is called, the current thread must synchronize on the lock object; wait() then releases this lock, and adds the thread to the "wait list" associated with the lock. Later, another thread can synchronize on the same lock object and call lock.notify(). This wakes up the original, waiting thread. Basically, wait()/notify() is like sleep()/interrupt(), only the active thread does not need a direct pointer to the sleeping thread, but only to the shared lock object.

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

    From this post : http://javaconceptoftheday.com/difference-between-wait-and-sleep-methods-in-java/

    wait() Method.

    1) The thread which calls wait() method releases the lock it holds.

    2) The thread regains the lock after other threads call either notify() or notifyAll() methods on the same lock.

    3) wait() method must be called within the synchronized block.

    4) wait() method is always called on objects.

    5) Waiting threads can be woken up by other threads by calling notify() or notifyAll() methods.

    6) To call wait() method, thread must have object lock.

    sleep() Method

    1) The thread which calls sleep() method doesn’t release the lock it holds.

    2) sleep() method can be called within or outside the synchronized block.

    3) sleep() method is always called on threads.

    4) Sleeping threads can not be woken up by other threads. If done so, thread will throw InterruptedException.

    5) To call sleep() method, thread need not to have object lock.

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

    Should be called from synchronized block : wait() method is always called from synchronized block i.e. wait() method needs to lock object monitor before object on which it is called. But sleep() method can be called from outside synchronized block i.e. sleep() method doesn’t need any object monitor.

    IllegalMonitorStateException : if wait() method is called without acquiring object lock than IllegalMonitorStateException is thrown at runtime, but sleep() method never throws such exception.

    Belongs to which class : wait() method belongs to java.lang.Object class but sleep() method belongs to java.lang.Thread class.

    Called on object or thread : wait() method is called on objects but sleep() method is called on Threads not objects.

    Thread state : when wait() method is called on object, thread that holded object’s monitor goes from running to waiting state and can return to runnable state only when notify() or notifyAll() method is called on that object. And later thread scheduler schedules that thread to go from from runnable to running state. when sleep() is called on thread it goes from running to waiting state and can return to runnable state when sleep time is up.

    When called from synchronized block : when wait() method is called thread leaves the object lock. But sleep() method when called from synchronized block or method thread doesn’t leaves object lock.

    For More Reference

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