what is the difference between sleep() and Wait()?
Answer Posted / agrawalamit166
Thread.sleep() sends the current thread into the "Not
Runnable" state for some amount of time. The thread keeps
the monitors it has aquired -- i.e. if the thread is
currently in a synchronized block or method no other thread
can enter this block or method. If another thread calls
t.interrupt() it will wake up the sleeping thread.
Note that sleep is a static method, which means that it
always affects the current thread (the one that is
executing the sleep method). A common mistake is to call
t.sleep() where t is a different thread; even then, it is
the current thread that will sleep, not the t thread.
object.wait() sends the current thread into the "Not
Runnable" state, like sleep(), but with a twist. Wait is
called on a object, not a thread; we call this object
the "lock object." Before lock.wait() is called, the
current thread must synchronize on the lock object; wait()
then releases this lock, and adds the thread to the "wait
list" associated with the lock. Later, another thread can
synchronize on the same lock object and call lock.notify().
This wakes up the original, waiting thread. Basically, wait
()/notify() is like sleep()/interrupt(), only the active
thread does not need a direct pointer to the sleeping
thread, but only to the shared lock object.
| Is This Answer Correct ? | 60 Yes | 12 No |
Post New Answer View All Answers
How will you reverse a singly-link list?
Which eclipse is best for java?
Give differences between Quicksort &Mergesort. When should these sorts be used andwhat is their running time in java?
What is locale?
What are the advantages of defining packages in java?
Explain notify() method of object class ?
What types of index data structures can you have in java?
What is the properties class?
What is the purpose class.forname method?
what is the purpose of "virtual"?
Similarity and difference between static block and static method ?
What is compareto () in java?
How to retrieve data from database in java using arraylist?
What is loop in java?
What is a module function?