Interview Questions

Difference between wait() and wait(long timeout), What are thread states when these method are called?

Java Interview Questions and Answers (part 4)


(Continued from previous question...)

50. Difference between wait() and wait(long timeout), What are thread states when these method are called?

wait()
wait(long timeout)
When wait() method is called on object, it causes causes the current thread to wait until another thread invokes the notify() or notifyAll() method for this object.
wait(long timeout) - Causes the current thread to wait until either another thread invokes the notify() or notifyAll() methods for this object, or a specified timeout time has elapsed.
When wait() is called on object - Thread enters from running to waiting state. It waits for some other thread to call notify so that it could enter runnable state.
When wait(1000) is called on object - Thread enters from running to waiting state. Than even if notify() or notifyAll() is not called after timeout time has elapsed thread will go from waiting to runnable state.

(Continued on next question...)

Other Interview Questions