What is the difference between a wait()
and sleep()
in Threads?
Is my understanding that a wait()
-ing Thread is still in runni
The fundamental difference is that wait()
is from Object
and sleep()
is a static method of Thread
.
The major difference is that wait()
releases the lock while sleep()
doesn’t release any lock while waiting.
wait()
is used for inter-thread communication while sleep()
is used to introduce a pause on execution, generally.
wait()
should be called from inside synchronise or else we get an IllegalMonitorStateException
, while sleep()
can be called anywhere.
To start a thread again from wait()
, you have to call notify()
or notifyAll()
. As for sleep(),
the thread gets started after a specified time interval.