Difference between wait() and sleep()

2018-12-31 03:48发布

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

Is my understanding that a wait()-ing Thread is still in running mode and uses CPU cycles but a sleep()-ing does not consume any CPU cycles correct?

Why do we have both wait() and sleep(): how does their implementation vary at a lower level?

30条回答
忆尘夕之涩
2楼-- · 2018-12-31 04:05
  1. wait() is a method of Object class.
    sleep() is a method of Thread class.

  2. sleep() allows the thread to go to sleep state for x milliseconds.
    When a thread goes into sleep state it doesn’t release the lock.

  3. wait() allows thread to release the lock and goes to suspended state.
    This thread will be active when a notify() or notifAll() method is called for the same object.

查看更多
低头抚发
3楼-- · 2018-12-31 04:07

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.

查看更多
余生无你
4楼-- · 2018-12-31 04:08

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.
查看更多
笑指拈花
5楼-- · 2018-12-31 04:09

wait and sleep methods are very different:

  • sleep has no way of "waking-up",
  • whereas wait has a way of "waking-up" during the wait period, by another thread calling notify or notifyAll.

Come to think about it, the names are confusing in that respect; however sleep is a standard name and wait is like the WaitForSingleObject or WaitForMultipleObjects in the Win API.

查看更多
余生请多指教
6楼-- · 2018-12-31 04:11

I found this post helpful. It puts the difference between Thread.sleep(), Thread.yield(), and Object.wait() in human terms. To quote:

It all eventually makes its way down to the OS’s scheduler, which hands out timeslices to processes and threads.

sleep(n) says “I’m done with my timeslice, and please don’t give me another one for at least n milliseconds.” The OS doesn’t even try to schedule the sleeping thread until requested time has passed.

yield() says “I’m done with my timeslice, but I still have work to do.” The OS is free to immediately give the thread another timeslice, or to give some other thread or process the CPU the yielding thread just gave up.

wait() says “I’m done with my timeslice. Don’t give me another timeslice until someone calls notify().” As with sleep(), the OS won’t even try to schedule your task unless someone calls notify() (or one of a few other wakeup scenarios occurs).

Threads also lose the remainder of their timeslice when they perform blocking IO and under a few other circumstances. If a thread works through the entire timeslice, the OS forcibly takes control roughly as if yield() had been called, so that other processes can run.

You rarely need yield(), but if you have a compute-heavy app with logical task boundaries, inserting a yield() might improve system responsiveness (at the expense of time — context switches, even just to the OS and back, aren’t free). Measure and test against goals you care about, as always.

查看更多
弹指情弦暗扣
7楼-- · 2018-12-31 04:11

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).

查看更多
登录 后发表回答