Difference between Wait and Sleep in Java

I am listing here differences between wait() and sleep() methods in java. Please feel free to suggest if you can think of anything apart from those listed below.

Wait Sleep
Wait method release the acquired monitor while the thread waits Sleep method holds the monitor while the thread goes to sleep
Wait method should be called from inside a synchronized block There is not such mandate for sleep method
wait() is an instance specific method and only resumes execution only if some other thread calls notify method on same object Thread.sleep() method is a static method and applies on current thread
In case of wait, waiting thread first acquires the lock and then goes into Runnable state In case of sleep, sleeping thread immediately goes to Runnable state after waking up
Use wait() method when you want to implement inter-thread communication. Use sleep method if you want your thread to pause for a fixed/known amount of time
Advertisements

, ,

  1. Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: