Tuesday, 27 March 2018

List out difference between wait and sleep in Java .


A) wait is called from synchronized context only while sleep can be called without synchronized block.
B) wait for release lock on an object while waiting while sleep doesn’t release lock while waiting.
C)waiting thread can be awake by calling notify and notifyAll while sleeping thread can not be awakened by calling notify method.
D)The wait() method  is called on an Object on which the synchronized block is locked, while sleep is called on the Thread.
E)wait is normally done on condition, Thread wait until a condition is true while sleep is just to put your thread on sleep.
F) Thread.sleep() method is a static method and applies on current thread, while wait() is an instance specific method and only got wake up if some other thread calls notify method on same object.
G)also, in the case of sleep, sleeping thread immediately goes to Runnable state after waking up while in the case of wait, waiting for a thread first acquires the lock and then goes into Runnable state

No comments:

Post a Comment

Difference between final, finally and finalize()?

final :Final is a keyword. Final is used to apply restrictions on class, method and variable. Final class can't be inherited, final ...