Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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

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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is charat java?

906


What will happen if non-synchronized method calls a static synchronized method and what kind of lock it acquires?

1000


What is defined as false sharing in the context of multithreading?

983


How many types of constructors are used in java?

1094


Is a boolean variable?

953


What is the primitive type byte?

940


Can we write a class without main method in java?

895


When should you make a function static?

928


can rmi and corba based applications interact ?

1020


What is one third plus one third as a fraction?

868


What does singleton class mean?

911


what is the purpose of "virtual"?

1016


What does int argc char * argv [] mean?

931


Is simpledateformat safe to use in the multithreaded program?

946


Does the order of public and static declaration matter in main method?

1029