Sunday, April 10, 2011

In Java Threads what is difference between the methods sleep, yield and wait ?


  • The sleep method would change the thread to sleep for the amount of time as passed in the parameter. E.g. sleep(1000) would cause the Thread to sleep for 1 second.

  • The wait method would cause the Thread to sleep for the amount of time passed as the parameter, but the Thread could wake up early if it recieves the notify() or notifyAll() method. E.g. if we call the method wait(1000) the Thread goes into sleep for 1 second, but might wake up in 0.5 seconds if it recieves a notify() or notifyAll() call.

  • The yield method changes the state of the object from running to runnable state.
The various possible states of Thread are posted here http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.State.html

No comments:

Post a Comment