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

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 ]

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

posted Aug 07, 2007 15:14:00

0

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.

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.