This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes A few questions on wait(), sleep(int) and InterruptedException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "A few questions on wait(), sleep(int) and InterruptedException " Watch "A few questions on wait(), sleep(int) and InterruptedException " New topic
Author

A few questions on wait(), sleep(int) and InterruptedException

Marc Wentink
Ranch Hand

Joined: May 18, 2007
Posts: 142
A few questions on Threads.

Since sleep(int) only effects the current Thread it is static. But the simular function in Object is not. Shouldn't wait() be static? According to the API it is not. Then can the method wait() cause another thread of execution to wait? That does not seems to be logical since it's purpose is to cause the current thread to wait until another thread invokes the notify() method. Is wait() under the hood calling something like synchronized(this) for the current object it belongs too? Then it would make sense not being static, since it needs 'this'.

Also wait() throws InterruptedException if another thread has interrupted the current thread. But when will this happen? Does it happen when it receives a notify?

And sleep(int) also throws InterruptedException, again how can another thread interrupt the sleep of the current thread? And it is remarkable since another thread cannot cause a Thread to sleep, but it can interrupt its sleep then?
[ August 07, 2007: Message edited by: Marc Wentink ]

SCJP5
christian combarel
Ranch Hand

Joined: Aug 04, 2007
Posts: 47
Hello,
Sleep and wait have not the same objectives. Sleep allow the current thread, as you said, to stop himself for a certain amount of time.
The wait method is called by a thread from an object instance whose it owns the lock so, wait can't be static.
Yes, the method wait() can cause another thread of execution to wait, as a thread is, after all, an object.

sleep() and wait() will throw an InterruptedException if the sleeping/waiting thread is interrupted by another thread invoking the interrupt method from this thread.



-------------
Chris
Marc Wentink
Ranch Hand

Joined: May 18, 2007
Posts: 142
Ah yes ok, I can see that the Thread class has a method interrupt:



public void interrupt()Interrupts this thread.
First the checkAccess method of this thread is called with no arguments. This may result in throwing a SecurityException.

Throws:
SecurityException - if the current thread cannot modify this thread.



This method is not static and you can do something like

Thread theOtherT = new Thread(myRunnable);
theOtherT.start();
// in his own run method theOtherT goes to sleep
theOtherT.interrupt();
// and theOtherT wakes up by the doing in this thread here.

Thank you for the clearance.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Originally posted by christian combarel:
Yes, the method wait() can cause another thread of execution to wait, as a thread is, after all, an object.
No! wait() causes a wait of the current thread until someone calls notify() or notifyAll() on the object, on that the thread waits.
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Hi

I saw the above responses and they seem to be correct to me though a bit ambiguous. I am putting in my thoughts below on this. Hope it helps.

Since sleep(int) only effects the current Thread it is static. But the simular function in Object is not. Shouldn't wait() be static?


1.wait causes the current thread to wait and give away the lock it holds. That's why wait is always called in a synchronized block on the locking object of this block. When some other thread using the same locking object calls notifyAll, all other threads waiting on this locking object need to be notified. So here you see that the method wait is very specific to the locking object and hence its a non-static method.

2.sleep causes the current thread to just sleep. No giving away of lock is associated. It just frees the processor to do other things otherwise it can be equated to something like an infinite loop. Once sleep is over same thread can continue processing as it still has the lock. Here we see that method sleep is not associted with any object and hence it is static.


According to the API it is not. Then can the method wait() cause another thread of execution to wait? That does not seems to be logical since it's purpose is to cause the current thread to wait until another thread invokes the notify() method.


I do not quite understand your question. wait always blocks the current thread in which it is called and never blocks any other thread.Technically wait() can be called on any object. But it makes sense to call wait() on a locking object. Because if you call wait on just any other object, first of all it will throw IllegalMonitorStateException if you are not doing this from a synchronized block. If you are doint it inside a synchronized block on a object, the current thread goes to an infinite wait until some other thread notifies on tje locking object.

Also wait() throws InterruptedException if another thread has interrupted the current thread. But when will this happen? Does it happen when it receives a notify? And sleep(int) also throws InterruptedException, again how can another thread interrupt the sleep of the current thread?


wait/sleep throw interrupted exception when another thread calls the interrupt method on the thread contaning the wait/sleep method. One important thing to note is that a thread in wait state gets back the lock once it is interrupted.

Try out this code which is an example of the discussion above:



Hope I am clear. Please post your queries on this thread if you find I am wrong or not clear somewhere.


Thanks and Regards
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: A few questions on wait(), sleep(int) and InterruptedException
 
Similar Threads
interrupt
INTERRUPTEDEXCEPTION
Thread
interrupt()
Threads Notes for Exams