What Is The Difference Between Wait() And Sleep() In Java?

7

7 Answers

Anonymous Profile
Anonymous answered
1) Sleep is a method of thread, wait is a method of object.
2) Interrupt() can be used to wake up a thread from sleep state, notify() and notifyAll() can be used to wake up from waiting state
3) Sleep method will not release the lock after completion of tine but wait  method does...
Gopinath Sundaram Profile
Wait waits till the other process is completed or a revoke(not sure about the method's name) is called,while sleep sleeps for the specified amount of period.
Anonymous Profile
Anonymous answered
And answer is wrong
tell me that
why wait leave the lock
and the sleep don't leave the lock
Anonymous Profile
Anonymous answered
The above answer is wrong as sleep function DOES take parameter.
Sleep(2000) means 2 seconds.
prasad khadkikar Profile
As we know java programming language supports multi threading concept. The threads are running at the same time if you want to stop execution of a thread and give the control to another thread these two functions, are supported by java for the purpose

Wait() : This function takes parameters as milliseconds and puts the thread in wait state for the desired time of the programmer after time passes the execution starts again.

Sleep() : This function is also used for same purpose using his function by java you can put a thread in sleep state .sleep does not contains any parameters so the thread will not be automatically start execution It needs a wake up signal again which can be Notify().or other function are also provided by java.

So the main difference in Wait() and sleep() is wait takes time parameter and wait for specific time only and sleep throws a thread in sleep mode for unspecified time.
Anonymous Profile
Anonymous answered
You have written that "sleep does not contains any parameters so the thread will not be automatically start execution"

I am adding the signature :
Public static native void sleep(long millis) throws InterruptedException;

Please have a look on signature and update the your answer

Answer Question

Anonymous