Java concurrency provides two methods, sleep() and wait() for controlling the execution flow of threads. While both methods appear similar in purpose, they play different roles.
In this Java tutorial, we will learn the difference between sleep() and wait() methods. We will also learn when to use which method and what effect they bring in Java concurrency.
1. Java Thread.sleep() Method
The Thread.sleep() is a static method that temporarily suspends its execution of the current thread for a specified time duration. During this time, the thread remains in the TIMED_WAITING
state.
- The sleep() method is best used in scenarios when we want to delay the execution of a thread for a specific period while waiting for a resource.
- It is quite common to use sleep() method for simulating a time-consuming operation or a time interval between tasks during unit testing.
import java.time.LocalDateTime;
public class SleepExample {
public static void main(String[] args) {
LocalDateTime start = LocalDateTime.now();
System.out.println("Thread started at: " + start);
try {
Thread.sleep(2000); // Sleep for 2 seconds
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalDateTime end = LocalDateTime.now();
System.out.println("Thread resumed after sleep at: " + end);
}
}
The program output:
Thread started at: 2022-05-12T09:30:45.123456
Thread resumed after sleep at: 2022-05-12T09:30:47.125789
In this example, the thread started at 2022-05-12T09:30:45.123456
and resumed after sleeping for 2 seconds at 2022-05-12T09:30:47.125789
. The actual timestamps will depend on the current time when the program is executed.
As the
sleep()
is astatic
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()
wheret
is a different thread; even then, it is the current thread that will sleep, not thet
thread.
2. Java Object.wait() Method
The wait()
method is a non-static method defined in the Object
class and used for inter-thread communication and synchronization. When a thread invokes wait()
on an object, it releases the lock on the object and enters the WAITING
state until another thread notifies it using the notify()
or notifyAll()
methods.
We use the wait() method generally to solve the producer-consumer pattern or other synchronization scenarios.
import java.time.LocalDateTime;
public class WaitExample {
public static void main(String[] args) {
Object lock = new Object();
LocalDateTime start = LocalDateTime.now();
System.out.println("Thread started at: " + start);
Thread waitingThread = new Thread(() -> {
synchronized (lock) {
try {
System.out.println("Waiting for notification at: " + LocalDateTime.now());
lock.wait(); // Wait until notified
System.out.println("Notification received at: " + LocalDateTime.now());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
Thread notifyingThread = new Thread(() -> {
synchronized (lock) {
try {
Thread.sleep(2000); // Simulate some processing time
lock.notify(); // Notify waiting thread
System.out.println("Notification sent at: " + LocalDateTime.now());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
waitingThread.start();
notifyingThread.start();
// Wait for threads to finish
try {
waitingThread.join();
notifyingThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
LocalDateTime end = LocalDateTime.now();
System.out.println("Thread finished at: " + end);
}
}
In this example:
- The program starts and prints the start timestamp.
- The waiting thread starts and prints the waiting timestamp.
- The notifying thread sends the notification after 2 seconds and prints the notification timestamp.
- The waiting thread receives the notification and prints the received timestamp.
- The program finishes and prints the finish timestamp.
The actual timestamps will depend on the current time wh
The program output:
Thread started at: 2022-05-12T09:30:45.123456
Waiting for notification at: 2022-05-12T09:30:45.123456
Notification sent at: 2022-05-12T09:30:47.125789
Notification received at: 2022-05-12T09:30:47.125789
Thread finished at: 2022-05-12T09:30:47.125789
Read more : Working with wait() and notify()
3. Difference between wait() and sleep() Method
The major difference is that wait()
releases the lock or monitor while sleep()
doesn’t release the lock while waiting. The wait()
is used for inter-thread communication while sleep()
is used to introduce pause on execution, generally.
The following table summarizes the main differences between the wait()
and sleep()
methods:
Feature | wait() | sleep() |
---|---|---|
Method Location | Defined in the Object class | Defined in the Thread class |
Usage | Used for inter-thread communication and synchronization | Used to pause the execution of a thread |
Lock Release | Releases the lock on the object being waited on | Does not release the lock |
Object Dependency | Called on an object within a synchronized block | Called directly on the Thread object |
Interruption | Throws InterruptedException when interrupted | Also throws InterruptedException when interrupted |
Notification | Requires notify() or notifyAll() to resume execution | Not applicable, thread resumes automatically after sleep |
Associated State | Puts the thread in the WAITING or TIMED_WAITING state | Puts the thread in the TIMED_WAITING state |
Time Specification | No time duration specified | Time duration specified in milliseconds |
Read more : Difference between yield() and join()
4. Summary
Let’s categorize all the above points in short to remember.
4.1. Method called on
wait()
– Call on an object; the current thread must synchronize on the lock object.sleep()
– Call on a Thread; always currently executing thread.
4.2. Synchronized
wait()
– when synchronized multiple threads access same Object one by one.sleep()
– when synchronized multiple threads wait for sleep over of sleeping thread.
4.3. Lock duration
wait()
– release the lock for other objects to have chance to execute.sleep()
– keep lock for at least t times if timeout specified or somebody interrupt.
4.4. wake up condition
wait()
– until call notify(), notifyAll() from objectsleep()
– until at least time expire or call interrupt().
4.5. Usage
sleep()
– for time-synchronizationwait()
– for multi-thread-synchronization.
Hope the above information will add some value in your knowledge-base.
Happy Learning !!
Comments