What is the difference between wait() and sleep() in Java?
139
19-Jul-2024
Updated on 19-Jul-2024
Ashutosh Kumar Verma
19-Jul-2024Difference between wait() and sleep()
Both
wait()
andsleep()
methods are used in Java for concurrent scheduling, but serve different purposes:wait()
Example-
sleep()
Example-
Specifically,
wait()
is used for communication between threads in synchronous context, whilesleep()
is used to pause execution for a specified period of time, regardless of sessionAlso, Read: What are the key features introduced in Java 8?