Difference between sleep and suspend?

Answer Posted / shweta

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between RMI and Corba?

2255


What are the different types of exception?

570


What is clustering? What are the different algorithms used for clustering?

576


Is the ternary operator written x : y ? Z or x ? Y : z ?

635


Why use a datasource when you can directly specify a connection details?

534






What is jboss?

571


what are getters and setters in Java with examples?

1284


What is bean? Where can it be used?

575


Which are the different segments of memory?

593


What is colon_pkg_prefixes and what is its use?

1995


In RMI, inorder to sent the stub reference to the client, is we have to load the server object first into the memory or can we directly sent reference to the client?

1555


How to pass parameters in RMI?

1686


What is the difference between session and entity beans?

578


What are the types of scaling?

552


What is a modular application?

588