what is the difference between sleep() and Wait()?

Answer Posted / amitasite

Thread.sleep() is static method which make current running
thread "not runnable" for specific time. Sleeping thread
doesn't release lock. It will transit to "ready to run"
state after specified time elapsed or other thread interrupts.

wait() can be call on shared object. Wait can be call only
if thread has lock. On calling thread it releases lock on
object and transit to "not runnable" state. It wake ups and
transit to "ready to run" state after other thread that got
lock call notify() or notifyAll() on shared object or call
interrupt().

Is This Answer Correct ?    11 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

State the merge-sort principle and its time complexity.

565


What is java and their uses?

544


What if constructor is protected in java?

556


Is hashmap thread safe?

550


Explain about anonymous inner classes in java?

572






Why does java have different data types for integers and floating-point values?

527


Does java set allow duplicates?

519


What is difference between static variable and global variable?

542


What is a null check?

531


What is map java?

509


What does sizeof return?

551


Why are arrays useful in java?

540


What is member in java?

484


Explain the difference between hashmap and hashtable in java?

485


Explain about class in java?

608