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

Answer Posted / suresh

Yes, sleep() and wait() methods are doing same work like prevent Thread Execution some amount of time in different ways.

where wait() is used only in Synchronization area this is called on object.wait(), it is overloaded method.if a thread object calls wait() then that thread will release the lock and wait for notification( notify(), notifyAll()) after getting notification thread will make Runnable.

where as sleep() is used any where it is static method. when sleep() called then thread will enter into sleep state particular amount of time if time expires thread will move into Runnable or Ready state.

In both cases Thread will Interrupted by interrupt().It will checked Exception we have take care of that with try n catch or throws.

Is This Answer Correct ?    5 Yes 1 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Write a program to show whether a graph is a tree or not using adjacency matrix.

596


Name some classes present in java.util.regex package.

608


What is bufferedwriter?

542


What is double parsedouble in java?

550


What is e in java?

543






Why are global variables used?

503


Are strings immutable in java?

524


Can we override singleton class?

538


What are JVM.JRE, J2EE, JNI?

693


what is deadlock? : Java thread

527


How do I convert a numeric ip address like 192.18.97.39 into a hostname like java.sun.com?

604


Does java initialize arrays to zero?

509


Discuss 2D arrays.

610


What is the purpose of the finalize() method?

713


What is a numeric format?

570