Java Threads Question:

Download Job Interview Questions and Answers PDF

What is the difference between the methods sleep() and wait()?

Java Threads Interview Question
Java Threads Interview Question

Answer:

The sleep method is used when the thread has to be put aside for a fixed amount of time. Ex: sleep(1000), puts the thread aside for exactly one second. The wait method is used to put the thread aside for up to the specified time. It could wait for much lesser time if it receives a notify() or notifyAll() call. Ex: wait(1000), causes a wait of up to one second. The method wait() is defined in the Object and the method sleep() is defined in the class Thread.

Download Java Threads Interview Questions And Answers PDF

Previous QuestionNext Question
Can Java object be locked down for exclusive use by a given thread?
Or
What happens when a thread cannot acquire a lock on an object?
What is the difference between process and thread?