It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Thread questions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Thread questions" Watch "Thread questions" New topic
Author

Thread questions

Bob Young
Ranch Hand

Joined: Dec 23, 2000
Posts: 65
I am reviewing Paul Hyde's Java Thread Programming book and I need some clarifications. In an alternative example to the use of the deprecated stop() method he uses this code snippet:
public void run() {
//note run() does not throw InterruptedException
// missing code
catch (InterruptedException x) {
Thread.currentThread().interrupt(): // reassert
}
In the discussion of the example he states: "If there were more interruptible statements like sleep() or wait(), they would not throw an InterruptedException unless the interrupt had been reasserted. You don't want them to throw an InterruptedException right away because the thread has been signaled to clean up and die."
I do not understand either of these two statements. If there were other sleep() or wait() statements, why couldn't they throw an InterruptedException unless reasserted? I am totally at a loss as to the meaning of the second sentence?
In looking at a Sun article: "Why Are Thread.stop, Thread.suspend, Thread.resume and Runtime.runFinalizersOnExit Deprecated?", I find the following that applies: "For this technique to work, it's critical that any method that catches an interrupt exception and is not prepared to deal with it immediately reasserts the exception. We say reasserts rather than rethrows, because it is not always possible to rethrow the exception. If the method that catches the InterruptedException is not declared to throw this (checked) exception, then it should "reinterrupt itself" with the following incantation:
Thread.currentThread().interrupt();
This ensures that the Thread will reraise the InterruptedException as soon as it is able. "
Why is it not always possible to rethrow the exception?
I noticed that in Paul's example, the run() method was not prepared to handle the InterruptedException and reinterrupted itself as per the Sun document. When interrupting itself in this fashion, the exception is passed up to the calling method, which would be start() and then main where start was invocked. Is this correct logic? Thanks in advance.
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Lets move this over to threads and see if you get some response over there.


"JavaRanch, where the deer and the Certified play" - David O'Meara
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Thread questions