Tuesday, August 18, 2015

sleep() vs wait()

Thread.sleep() is used for time-synchronization.
Thread.sleep(5000); // no synchronization needed

object.wait() is used for multi-thread-synchronization.
synchronized (lock) {
  while (!condition) {
    lock.wait(); // releases lock
  }
}
synchronized (lock) {
  lock.notify();
}

No comments: