Difference Between sleep() and wait() in Java

๐Ÿ’ก Concept: sleep() vs wait()

sleep() is a static method in Thread class that pauses the thread for a given time. wait() is an instance method of Object used for inter-thread communication.

๐Ÿ“˜ Quick Intro

sleep() is used to delay thread execution, while wait() is used to pause execution until another thread notifies it.

๐Ÿง  Analogy

sleep() is like putting an alarm clock and sleeping for a fixed time. wait() is like waiting for someone to tap your shoulder before you can continue.

๐Ÿ”ง Technical Explanation

  • sleep() does not release the monitor lock.
  • wait() releases the lock and waits until notify() or notifyAll() is called.
  • sleep() is used for delaying execution.
  • wait() is used for coordination between threads.
  • wait() must be called within a synchronized context.

๐ŸŽฏ Use Cases

  • โœ… Use sleep() to pause execution unconditionally for a duration.
  • โœ… Use wait() when a thread needs to pause until a condition is met by another thread.
  • โœ… Ideal for producer-consumer problem using synchronized blocks.

๐Ÿ’ป Example: sleep() vs wait()


public class SleepVsWait {
    public static void main(String[] args) {
        Object lock = new Object();

        Thread t1 = new Thread(() -> {
            synchronized(lock) {
                try {
                    System.out.println("Thread waiting...");
                    lock.wait();
                    System.out.println("Thread resumed!");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2 = new Thread(() -> {
            synchronized(lock) {
                try {
                    Thread.sleep(1000);
                    lock.notify();
                    System.out.println("Thread notified waiting thread");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        t1.start();
        t2.start();
    }
}

โ“ Interview Q&A

Q1: What is the purpose of sleep()?
A: Pauses thread for a fixed time without releasing lock.

Q2: What does wait() do?
A: Pauses and releases the monitor lock until notified.

Q3: Is sleep() part of Object class?
A: No, it's in Thread class.

Q4: Can wait() throw exceptions?
A: Yes, InterruptedException.

Q5: Can sleep() be called without synchronization?
A: Yes.

Q6: Can wait() be used outside synchronized block?
A: No, throws IllegalMonitorStateException.

Q7: Does sleep() release the lock?
A: No.

Q8: Can notify() work without wait()?
A: Yes, but it has no effect if no thread is waiting.

Q9: Is wait() overloaded?
A: Yes, with timeout and nanos.

Q10: Can both be interrupted?
A: Yes.

๐Ÿ“ MCQs

Q1. Which method releases the monitor lock?

  • sleep()
  • wait()
  • join()
  • notify()

Q2. What class contains sleep()?

  • Object
  • Thread
  • Runnable
  • Executor

Q3. Is synchronization needed for sleep()?

  • Yes
  • No
  • Sometimes
  • Only in threads

Q4. What exception can wait() throw?

  • IOException
  • NullPointerException
  • InterruptedException
  • IllegalStateException

Q5. Does sleep() pause for fixed time?

  • No
  • Yes
  • Depends
  • Only in threads

Q6. Can wait() be used outside synchronized?

  • Yes
  • No
  • Sometimes
  • With lock object

Q7. Which method is used for communication between threads?

  • sleep()
  • yield()
  • wait()
  • exit()

Q8. What does notify() do?

  • Starts new thread
  • Pauses thread
  • Wakes a waiting thread
  • Cancels thread

Q9. Does sleep() release CPU?

  • No
  • Yes temporarily
  • Forever
  • Only on wait

Q10. Does wait() block forever without notify()?

  • No
  • Yes
  • Sometimes
  • If lock is free

๐Ÿ’ก Bonus Insight

Use Condition objects from java.util.concurrent.locks for advanced wait-notify patterns with better control than synchronized blocks.

๐Ÿ“„ PDF Download

Need a handy summary for your notes? Download this topic as a PDF!

๐Ÿ” Navigation

Share:

Tags:


Feedback Modal Popup