Friday, October 2, 2015

What is the difference between sleep() and wait() in Java?

sleep() 
  • It is the blocking operation which keeps a holding on the screen or locking of a shared object properties while a process execution for the specified number of milliseconds.
  • It is most commonly used for polling during execution.
  • It is used to check for certain results after specific interval during process execution.
wait() 
  • It simply stops / pauses the process execution thread until  given number of milliseconds to be elapsed  or after receiving a notification from parent process execution without keeping a holding or locking of the shared object.
  • It is generally used in multi threaded applications where as required to interaction with notify() / notifyAll() to achieve synchronization during process execution.