Doin' Java to be one of the cool kids.
I usually use Perl;
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.
interrupt
public void interrupt()
Interrupts this thread.
Unless the current thread is interrupting itself, which is always permitted, the checkAccess method of this thread is invoked, which may cause a SecurityException to be thrown.
If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.
Doin' Java to be one of the cool kids.
I usually use Perl;
See Furst wrote:and you two were paying too much attention to mis-spellings and my comments about javadocs to think about the question.
See Furst wrote:The answer, I believe, is, as I have read in the javadocs is that interrupt() is not called on the thread, which seems to be the only thing that will cause a sleeping or waiting thread to throw an InterruptedException()
See Furst wrote:My next question is... a thread has to interrupt itself to get that code executed? OR another thread has to interrupt the one that is waiting? My assumption is that the code is just bad design, which most code for this test seems to be (maybe it's just this book?)
Mike Simmons wrote:
See Furst wrote:and you two were paying too much attention to mis-spellings and my comments about javadocs to think about the question.
Hardly. Paul pointed you directly at the answer.
See Furst wrote:The answer, I believe, is, as I have read in the javadocs is that interrupt() is not called on the thread, which seems to be the only thing that will cause a sleeping or waiting thread to throw an InterruptedException()
Correct. Glad you were able to see it by looking again.
See Furst wrote:My next question is... a thread has to interrupt itself to get that code executed? OR another thread has to interrupt the one that is waiting? My assumption is that the code is just bad design, which most code for this test seems to be (maybe it's just this book?)
It looks like you, or someone, has miscopied the code. Looking at the SCJP 6 book, p. 753, I see:
The most important difference here is that in the "Total is:" line is outside the catch block. The author knew that InterruptedException should never be thrown, since there is no interrupt(). The catch block is only there to make the compiler happy; it should never get executed. Instead the "Total is:" line should be executed shortly after c.wait() finishes, which should be shortly after the call to notifyAll() from the other thread.
Also, sometimes code will intentionally contain bad code in order to discuss a point or teach you something. Bad designs occur in real life, after all, and part of programming is learning to figure out what the problem is, and how to fix it. In this case, there's a problem in the code because the call to wait() is not contained in a loop - this is discussed in the very next section of the book.
Doin' Java to be one of the cool kids.
I usually use Perl;