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

Answers were Sorted based on User's Feedback



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

Answer / 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

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

Answer / deepak divvela

The main difference is if we need the thread wants to block
particular period of time we just choose sleep().
and when the thread called wait() means it will blocked
untill we will call notify(),notifyAll();

Is This Answer Correct ?    49 Yes 14 No

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

Answer / vinay kumar

sleep() will wait go to sleep for amount of time that is specified in it.After the times finishes it resumes automatically.

wait() will also wait for some I/O and will NOT resume automatically but will resume only when notify() or notifyall() is called.

Is This Answer Correct ?    19 Yes 7 No

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

Answer / 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

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

Answer / kumaran

thread.sleep(2000) - this will block the thread exactly 2000
milliseconds.

thread.wait(2000) - this will block the thread upto 2000
milliseconds.

i.e if thread.notify() calls before 2000 milliseconds, the
thread will become active incase if the thread is blocked by
wait().

Is This Answer Correct ?    14 Yes 7 No

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

Answer / tisty

Simply speaking, we will use sleep() for pausing a thread
intentionally for some amount of time which we mentions in
milliseconds as the argument of sleep() method and,
wait() method is used whenever there is a communication is
needed between the threads, say if one thread's execution
is depended on the the execution of others.

The thread went on sleep will continue its execution after
the timeperiod. on the other hand, the thread undergone
wait will get activated on a notify() or notifyall() method.

Is This Answer Correct ?    6 Yes 2 No

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

Answer / 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

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

Answer / sat

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.

t.suspend() is deprecated. Using it is possible to halt a
thread other than the current thread. A suspended thread
keeps all its monitors and since this state is not
interruptable it is deadlock prone.

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 ?    1 Yes 0 No

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

Answer / vijay

Sleep():

Pause for NUMBER seconds. SUFFIX may be 's' for seconds
(the default), 'm' for minutes, 'h' for hours or 'd' for
days. Unlike most implementations that require NUMBER be
an integer, here NUMBER may be an arbitrary floating point
number.


Wait():

The wait function suspends execution of the current
process until a child has exited, or until a signal is
delivered whose action is to terminate the current
process or to call a signal handling function. If a child
has already exited by the time of the call (a so-
called "zombie" process), the function returns
immediately. Any system resources used by the child are
freed.

Is This Answer Correct ?    5 Yes 14 No

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

Answer / modi[achir communication pvt.

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.

Is This Answer Correct ?    6 Yes 105 No

Post New Answer

More Core Java Interview Questions

What is the meaning of immutable regarding string?

0 Answers  


What are the loops in java?

0 Answers  


What is the importance of hashcode() and equals() methods?

0 Answers  


Explain java coding standards for interfaces?

0 Answers  


What is callable java?

0 Answers  






What do you understand by Header linked List?

0 Answers   Genpact,


how to know the total memory occupied by the objects in the ArrayList(Array list may contain duplicate objects)

0 Answers   Infosys, TCS,


What happens when you add a double value to a string?

0 Answers  


What are the approaches that you will follow for making a program very efficient?

0 Answers  


Explain about map interface in java?

0 Answers  


What are 5 boolean operators?

0 Answers  


What is clipping?

2 Answers  


Categories