File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread" Watch "Thread" New topic
Author

Thread

Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
Can anybody tell me the situations at which InterruptedException is thrown. I know wait() and sleep() throws InterruptedException.
I have read somewhere that ctually interrupt() causes InterruptedException(I don't remember where I read it).
My understanding :-
wait() causes InterruptedException when some other Thread's notify() or notifyall() is called.
sleep() causes interrupt to be called on the Thread which causes InterruptedException.
So I think whenever InterruptedException is thrown,Thread is stopped.
One qn:
What might cause the current thread to stop executing?
A. An InterruptedException is thrown
B. The thread executes a sleep() call
C. The thread constructs a new Thread
D. A thread of higher priority becomes ready (runnable)
E. The thread executes a read() call on an InputStream
I think A,B,D,E are correct.
Can anybody give a confirmation?
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
I request others also to participate. Consider this as your own doubt and try to help Mani.
Thank you all.
regds
maha anna
sreelakshmi sarma
Ranch Hand

Joined: Mar 02, 2000
Posts: 130
Mani,
I think InterruptedException is thrown by interrupt() not by sleep() or wait(). That's the reason you have to put these method calls in a try&catch block. I think if interrupt() has not been called on the waiting or sleeping thread then that thread will never throw InterruptedException. what i understood from a thread stops executing if an interrupted exception is thrown is the thread which invokes interrupt() on another waiting or sleeping thread could stop executing if the thread which received interrupt() call comes out of waiting or sleeping state has higher priority than the thread which invoked interrupt() and becomes runnable.
Please correct me if am wrong.
Thanks.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

My understanding :-
wait() causes InterruptedException when some other Thread's notify() or notifyall() is called.

I don't think so. When the waiting thread is notified,
IMO, it is not an interruption. It is excepted and the
waiting thread becomes Ready.

sleep() causes interrupt to be called on the Thread which causes InterruptedException.

Sorry, I din't get this question .....

So I think whenever InterruptedException is thrown,Thread is stopped.

I would agree to this.

What might cause the current thread to stop executing?
A. An InterruptedException is thrown
B. The thread executes a sleep() call
C. The thread constructs a new Thread
D. A thread of higher priority becomes ready (runnable)
E. The thread executes a read() call on an InputStream
I think A,B,D,E are correct.
Can anybody give a confirmation?

My answers would be A, B, E.
About C, I am only creating an new instance of Thread,
I do not see any reason why the current executing thread
should stop.
About D. Even if another thread gets higher
priority, I don't think the current thread will STOP.
Think about it this way, "Why do I need an yield() method
if this was true?"
IMO, an executing thread stops only due to a call to:
yield(), stop(), read(), InterreptedException, suspended()
wait(); sleep().
May be more ....
Hope it help.
Regds.
- satya


[This message has been edited by satya5 (edited April 27, 2000).]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944

sree:
Acc. to Java 2 API,
interrupt() throws SecurityException
sleep() and wait() throw InterruptedException.
When a waiting thread is interrupted by another thread,
then this exception is raised. The exception is not
thrown until the lock status of this object has been
restored.
Same with sleep(), ofcourse there is not lock stuff with
sleep().
Correct me if I am wrong.
Regds.
- satya
sathish
Ranch Hand

Joined: Apr 25, 2000
Posts: 33
Mani..
In my view, there are only three states where a thread can be stopped.
1. stop() method (jdk1.1) which is now deprecated.
(or)
when a interrupt method is called on a thread, the thread breaks out of a sleep() or wait() and a interrupted exception is thrown
to be more clear, When the interrupt method is called on a thread object that is currently blocked, the blocking call (such as sleep or wait) is terminated by an interrupted exception.
But there is no Hard and Fast rule or no language requirement that a thread that is interrupted should end or terminate!
There are several instances where in some threads which are really important that they might simply ignore their interruption by catching the exception and would continue!. But mostly, a thread will simply interrupt an interruption as a request for termination.
2. Threads which are blocked or in a blocked state are said to be temporarily stopped ( Sleep, wait or even if a thread is waiting for an object lock that is owned by another thread, then the other thread must have given up the lock, or a thread which waits for an IO operation such as read())
3.when a higher priority thread is available.( i would say runnable and not available)
A higher priority thread is runnable or only runnable until it relinquishes by a yield() or till it naturally ceases to be runnable and this may be by entering a blocked state or when another thread with a higher priority becomes runnable.
i hope this is clear for you.
correct me if i am wrong!
thanks
sathish
maha anna
Ranch Hand

Joined: Jan 31, 2000
Posts: 1467
Mani,
The situations at which InterruptedException is thrown
1. InterruptedException is checked Exception right? So all the Thread related methods which have to be kept inside a try-catch-InterruptedException have to be considered. If you take the Thread class there are sleep(..) ,join(..) are the only methods which throw Interrupted Exception. Since Thread class is direct subclass of Object class we have to verify in Object class's methods also since they too are inherited to Thread class.
In Object class there is only one method which is wait(...) which has to be kept inside a try-catch-InterruptedException block. So now we have totally 3 methods in our hand which may throw InterruptedException when called on an object.
1. sleep(..) when you call myT.sleep(..) this means the thread myT just sleeps for so much (arg) secs doing nothing. Also remember it still hold the locks if it has any tightly . Also note that in order to sleep(..) , the myT doesn't need to obtain the objects lock first. It is the wait(..)/notify()/notifyAll() set of methods which have to obtain the lock first. So this sleeping thread is in waiting state and it may come out of the sleep mode in 2 ways. one, it wakes up on its own. The other, some other thread , say it is otherT ,interrupts its sleep by invoking myT.interrupt() in one of its methods inside otherT's class. For the 2nd case, if the sleeping thread is in fact interrupted, then an InterruptedException is thrown and this sleeping thread comes out of waiting state and goes directly to Ready state. The next time when the Thread scheduler schedules this woke up thread for execution, then only the catch(InterruptedException e) { } block will be executed. So this is one situation where an InterruptException may be thrown by a thread.
2. The other method is join(..) . This method also has to be kept inside try-catch-InterruptedException block , and when this method is called on a thread object, say mtT.join(..), then myT waits for the death of its own upto so much secs. When you put myT.join(1000) , it means myT thread waits for a second of there is any chance of death of the thread , if not, it continues. During this wait mode if some other thread interrupts by calling interrupt() on this thread, then InterruptedException will be thrown. join() without arg means wait forever for this thread to die.
Try this
<pre>
class Test extends Thread {
public void run() {
for(int i=0; i<100; i++) {
System.out.println("i="+i);
if(i==50) {
try {
this.join(1000);
//Thread.currentThread().join(1000);
}catch(InterruptedException e) {
System.out.println(e);
}
}
}
}
public static void main(String[] args) {
new Test().start();
}
}
</pre>
3. The 3rd method is wait(..)
This again has to be inside try-catch-Inter...Excep.. block. A thread myT ,which called wait(..) may throw InterruptedException in 3 occations.
(i) When another thread calls myT.interrupt()
(ii) When another thread calls myT.notify()
(iii)When another thread calls myT.notifyAll()
Also note that the myT thread which called wait() had immediately released all the locks which it obtained on this object. The outsider thread which calls notify()/notifyAll() MUST have the SAME OBJECT'S LOCK which the waiting thread had released before. Also note that there is a pool of waiting threads for each object which are just waiting to somhow obtain the lock and want someone to notify. In order to notify the correct set of waiting pool of threads , the notifying thread mUST have the SAME LOCK which the waiting threads expect. Then only there will be synchronization in talk. No miss communication.
The other case which is through calling the interrupt() on a waiting thread also can throw InterruptedException.

wait() causes InterruptedException when some other Thread's notify() or notifyall() is called.
This statement of yours slightly has to be changed. It is NOT when some other Thread's notify or notifyAll() is called. It is when some other Thread calls notify/notifyAll()
sleep() causes interrupt to be called on the Thread which causes InterruptedException. sleep() on its own does not cause any exception. It is when some other thread interrupts this sleeping thread.
So I think whenever InterruptedException is thrown,Thread is stopped.
No. It is not correct. There is no direct connection between InterruptedException and a thread being stopped. IT is those three methods sleep(..)/wait(..)/join(..) which causes the thread to stop.
Other than all these possiblities, a thread may be stopped when its stop()/suspend()/ is called. Also when the thread is blocked on something to happen. For example reading a block of data over the internet from a far-away remote server. One more possiblity is When another thread of higher priority thread comes to the Ready state. All these discussion , I assumed stop() means either temporary or permanent stop of a thread.

Satya,
interrupt() causes SecurityException means, the outsider thread which invokes the interrupt on our poor thread myT only throws SecurityException. THis does not mean our interrupted myT thread ,throws the SecurityException. Do you get the point?. Any thread can't interrupt any other thread just like that. The thread which is going to interrupt() another thread , first has to undergo some security checks by JVM. If it does not pass this check, then the outsider thread has no rights to interrupt our myT thread.
regds
maha anna

[This message has been edited by maha anna (edited April 27, 2000).]
Mani
Ranch Hand

Joined: Apr 20, 2000
Posts: 50
Excelent explanation Maha. Thanks alot.
Rosemol Thanjappan
Ranch Hand

Joined: Aug 08, 2000
Posts: 40
Hi Maha,
Are you sure about these situations cause
InterruptedException?

IMHO they won't .

(ii) When another thread calls myT.notify()
(iii)When another thread calls myT.notifyAll()


From java API :
public class InterruptedException extends Exception.
Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.
Please check it out.
Originally posted by maha anna:

3. The 3rd method is wait(..)
This again has to be inside try-catch-Inter...Excep.. block. A thread myT ,which called wait(..) may throw InterruptedException in 3 occations.
(i) When another thread calls myT.interrupt()
(ii) When another thread calls myT.notify()
(iii)When another thread calls myT.notifyAll()

[This message has been edited by maha anna (edited April 27, 2000).]

Thanking you .
Rosemol.
Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
yae maha pls clarify. like rosemol, i'm also a bit skepticabout a thread being "interrupted" by notify/notifyall.
Junaid Bhatra
Ranch Hand

Joined: Jun 27, 2000
Posts: 213
Maha,
Great explanation (as always). However I'm quite sure that notify() or notifyAll() does NOT cause an InterruptedException to be thrown. If a thread is notified, it will try to acquire the lock, and continue from the point where it left off. Only if you call interrupt() on a thread, will it throw an InterruptedException, that too only when that thread is either in wait(), join() or sleep() conditions.
So what happens if a thread is currently executing and another thread calls an interrupt() on it? This also does NOT cause an InterruptedException. However the interruptedStatus flag of that thread is set to true. So the next time this thread goes into a wait() or a sleep(), an InterruptedException will be thrown and the interruptedStatus will be set to false.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread