What is difference between sleep() and wait() method in Java?

Upasana | April 23, 2019 | 2 min read | 902 views | Multithreading and Concurrency


wait() and notify() are used in multi-threading programming for inter-thread communication, while sleep is used to control execution time of current thread.

Summary of difference b/w sleep() and wait()

  1. sleep() method is part of java.lang.Thread class

    private void sleepExample()
      throws InterruptedException {
        Thread.sleep(1000);
      }

    wait() is method of Object class, though Thread class also has wait() method since it inherits from Object too, but wait() is called on the synchronized object.

    Object lock = new Object();
    synchronized (lock) {
        lock.wait();
    }
  2. sleep() may be called from anywhere. wait() method must be called from synchronized context (i.e. synchronized method or block), otherwise it will throw IllegalMonitorStateException

  3. sleep() does not release the lock acquired by thread on current object. wait() releases the lock on object waiting for a condition to be fulfilled, in the meantime another thread can acquire the lock on this object.

  4. sleeping thread can not be woken up (except by calling Thread.interrupt()). A wait can be "woken up" by another thread calling notify() or notifyAll() on the monitor which is being waited. If more than one thread are waiting on the given object, only one of them will be able to grab the lock.

    synchronized (lock) {
        lock.notifyAll();
    }
  5. spurious wakeups (waiting thread wakes up for no apparent reason) can happen on wait(), so you should always call wait() inside a while loop on some condition.

    synchronized (lock) {
        while (!condition) {
         lock.wait();
        }
        //Take action
    }

    Such wakeups never happen in sleep().

  6. Thread goes from Runnable to TIMED_WAITING state. Thread goes from Runnable to WAITING state, if no timeout is specified

The below example of CustomSemaphore illustrates use of wait() and notify() method on a object monitor in synchronized context.

Custom Semaphore using wait() and notify()
public class CustomSemaphore {
    private final Object monitor = new Object();
    private int permits;

    CustomSemaphore(int initialPermits) {
        synchronized (monitor) {
            permits = initialPermits;
        }
    }

    /**
     * Blocks until permitsAvailable (permit > 0)
     */
    public void acquire() throws InterruptedException {
        synchronized (monitor) {
            while (permits <= 0)
                monitor.wait();
            --permits;
        }
    }

    /**
     * Release a single permit and notifies threads waiting on permitsAvailable Condition
     */
    public void release() {
        synchronized (monitor) {
            ++permits;
            monitor.notifyAll();
        }
    }
}

In general, we should use sleep() for controlling execution time of one thread and wait() & notify() for multi-thread synchronization.


Top articles in this category:
  1. What happens when wait() & notify() method are called
  2. What is difference between HashMap and HashSet
  3. What is difference between Vector and ArrayList, which one shall be preferred
  4. Difference between ExecutorService submit and execute method
  5. Difference between Callable and Runnable Interface
  6. Can two threads call two different synchronized instance methods of an Object?
  7. What is purpose of Collections.unmodifiableCollection

Recommended books for interview preparation:

Find more on this topic:
Buy interview books

Java & Microservices interview refresher for experienced developers.