Thursday, January 23, 2014

Difference between sleep() and wait()?

Call on:
  •     wait(): Call on an object; current thread must synchronize on the lock object.
  •     sleep(): Call on a Thread; always currently executing thread.
Synchronized:
  •     wait(): when synchronized multiple threads access same Object one by one.
  •     sleep(): when synchronized multiple threads wait for sleep over of sleeping thread.
Hold lock:
  •     wait(): release the lock for other objects to have chance to execute.
  •     sleep(): keep lock for at least t times if timeout specified or somebody interrupt.
Wake-up condition:
  •     wait(): until call notify(), notifyAll() from object
  •     sleep(): until at least time expire or call interrupt().
Usage:
  •     sleep(): for time-synchronization and;
  •     wait(): for multi-thread-synchronization.

No comments:

Post a Comment