In this post, we will learn the difference between the "wait()" and "sleep()" methods in Java. This is a frequently asked question in Java interviews for beginners. Let's dive into it.
Feature | wait() method | sleep() method |
---|---|---|
Class | Defined in the Object class. | Defined in the Thread class. |
Usage | Used for inter-thread communication. | Used for suspending the current thread. |
Locking | Releases the monitor lock of the object it's called on and waits to be notified by another thread. | Does not release any locks. |
Syntax | synchronized (object) {
while (condition) { object.wait(); } } |
try {
Thread.sleep(milliseconds); } catch (InterruptedException e) { //Handle the exception. } |
Exception Handling | Requires handling InterruptedException to be handled by the calling code. | Requires handling InterruptedException to be handled by the calling code. |
Use with Synchronized Blocks | Typically used within synchronized blocks to wait for a condition to be met. | Can be used outside synchronized blocks to introduce delays in the thread execution. |
Purpose | To allow threads to wait for specific conditions before continuing their execution. | To introduce a pause or delay in the execution of the current thread. |
Release Condition | The "notify()" or "notifyAll()" method must be called by another thread to wake up the waiting thread(s). | Sleep duration determines when the thread resumes its execution. |
Execution Time | The waiting thread can be woken up at any time by a notification or interrupt signal. | The sleeping thread will resume its execution after the specified sleep duration. |
Object Dependency | Depends on the specific object's monitor lock for synchronization. | Does not depend on any specific object and can be called on any thread. |
Example
Let's create an example to demonstrate the difference between wait() and sleep() in Java.
wait():
wait() is a method defined in the Object class in Java and is used for inter-thread communication. It allows a thread to release the lock on an object and wait until another thread notifies it to resume. wait() must always be used within a synchronized block or method.
public class WaitSleepExample {
public static void main(String[] args) {
final Object lock = new Object();
Thread thread1 = new Thread(() -> {
synchronized (lock) {
System.out.println("Thread 1 is waiting...");
try {
lock.wait(); // Thread 1 waits until it's notified by Thread 2
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 1 is resuming.");
}
});
Thread thread2 = new Thread(() -> {
try {
Thread.sleep(2000); // Sleep for 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lock) {
System.out.println("Thread 2 is notifying.");
lock.notify(); // Notifying Thread 1 to resume
}
});
thread1.start();
thread2.start();
}
}
Output:
Thread 1 is waiting...
Thread 2 is notifying.
Thread 1 is resuming.
sleep():
sleep() is a static method defined in the Thread class and is used to pause the execution of a thread for a specified amount of time. It does not release any locks.
public class WaitSleepExample {
public static void main(String[] args) {
Thread thread1 = new Thread(() -> {
System.out.println("Thread 1 is sleeping...");
try {
Thread.sleep(2000); // Sleep for 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 1 has woken up.");
});
Thread thread2 = new Thread(() -> {
System.out.println("Thread 2 is sleeping...");
try {
Thread.sleep(2000); // Sleep for 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Thread 2 has woken up.");
});
thread1.start();
thread2.start();
}
}
Output:
Thread 1 is sleeping...
Thread 2 is sleeping...
Thread 1 has woken up.
Thread 2 has woken up.
Interview Questions
Java
X vs Y
Comments
Post a Comment