Hi, if someone can explain the exact difference between the sleep() and wait() methods of threads.. Wali
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
hi, in sleep() a thread "sleeps" for the specified amount of time and it keeps the locks on any object that it currently holds. in wait() the thread "waits" until another thread notifies it useing notify or notifyAll() to wake up. in this case the object relenquishes control over any locks that it currently holds. Rahul
maha anna
Ranch Hand
Joined: Jan 31, 2000
Posts: 1467
posted
0
Rahul, what you said is as such correct. Also there is also 2 more wait(long timeout) and wait(long timeout, int nanosec) types. These 2 types are used for the safer side. What if Nobody notifies this waiting thread at all ? It has to wait forever right? So in order to just wait for some time, if someother thread notify()/notifyAll() it is ok. If not, after the specified amt of time is elapsed, it is taken out of the wait set of the object, for whose lock it is waiting for by means of some other thread notify()/notifyAll(), it goes straight to ready state regds maha anna
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Thanks Rahul and Maha
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
fyi sleep() is a static mehtod in the Thread class * put the current thread to sleep for a period of time the thread is in the wait state. after sleeping, the thread is moved from a wait to a ready state. yield() is a static method in the Thread class * moves the current thread from a runing state to a ready state wait() is a method in the Object class * moves the current thread into a wait state until .. notify() notifyall() interrupt() or System.exit() notify() is a method in the Object class * moves one thread from a wait to a seeking lock state once a lock is obtained, by one thread choosen at random the thread will be moved into a ready notifyall() is a method in the Object class * moves one or more thread(s) from a wait to a seeking lock state once a lock is obtained, by one thread choosen at random the thread will be moved into a ready let me know if i have missed anything, Monty
Anonymous
Ranch Hand
Joined: Nov 22, 2008
Posts: 18944
posted
0
Another related method ..... suspend() is a (depricated) method of the thread class. This will also stop a thread from running. But the important point is it can be resume()d by another thread. suspend() is not a permenant way to stop a thread. (and who says deprecated methods are not asked ... ) Regds. - satya
[This message has been edited by satya5 (edited June 08, 2000).]
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.