Monday, December 5, 2016

yield() vs join() vs sleep()

This is another popular core interview question. Look at the following table.


yield() vs sleep()

  • Both methods are used to stop execution of current thread.
  • But the different is, yield() method release its lock of object while sleep() method doesn't release its lock of object. 

wait() vs sleep()

  • sleep() method allows to thread to go to sleep state for given time. As you know, it doesn't release the lock of object.
  • wait() allows thread to release the lock and goes to blocked state. It can be active only by calling notify() or notifyAll() methods.

notify() vs notifyAll()

  • notify() method activate the first thread which called wait().
  • notifyAll() method activate all threads which called wait(), highest priority thread will run first.