Difference between wait() and sleep() - Java

Difference between wait() and sleep()


  • The fundamental difference is wait() is from Object and sleep() is static method of Thread.
  • The major difference is that wait() releases the lock while sleep() doesn’t release any lock while waiting.
  • The wait() is used for inter-thread communication while sleep() is used to introduce pause on execution, generally.
  • The wait() should call from inside synchronize or else we get IllegalMonitorStateExceptionwhile sleep() can call anywhere.
  • To start thread again from wait(), you have to call notify() or notifyAll(). While in sleep(), thread gets start after specified ms/sec interval.

Comments

Popular posts from this blog

EJB - Stateful vs Stateless

Mirror binay tree - Java