Sorry, this question is not related to the task, I just do not know where to put it.
Regarding this simple code, why after throwing an exception, it keeps printing "Tick" ?
public class Main {
public static void main(String[] args) throws InterruptedException {
Clock clock = new Clock();
Thread clockThread = new Thread(clock);
clockThread.start();
Thread.sleep(2000);
clockThread.interrupt();
}
static class Clock implements Runnable{
public void run()
{
Thread current = Thread.currentThread();
while (!current.isInterrupted())
{
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Tick");
}
}
}}
A question
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Nouser
17 March 2021, 10:32
The interrupterException gets resetted (set back to false) after being caught in the catch block. When the loop starts a new iteration then the loop condition is true again and the game continues.
The options you have...
1. interrupt again in the catch block
2. break out of the loop (in the catch block)
3. have the while loop inside the try block
btw. that drove me nuts, too :)
+1
Mina Nabil
17 March 2021, 10:49
Thanks for the answer Nouser :) . Regarding this
"The interrupterException gets resetted (set back to false) after being caught in the catch block"
Is that normal for exceptions, I mean do they normally react like this"resetting" ? If you could point me to an article or any document, that would be great.
0
Nouser
17 March 2021, 13:16
Afaik that is a feature of the isInterrupted method. Once you (or the sleep method) gets a true from isInterrupted, it gets set back to false. Don't know what I have written above. Seems I wanted to write something different :)
As source I can offer you just a german book.
https://openbook.rheinwerk-verlag.de/javainsel9/javainsel_14_003.htm#mj4193c6da30b5a3e9d41c167ec04d571f
The relevant part is after the code (online translator):
The run() method in the thread is implemented in such a way that the loop is exited exactly when isInterrupted() returns the value true, i.e. the interrupt() method was called from outside for this thread instance. This is exactly what happens in the main() method. At first glance, the program is easy to understand, but it is probably the interrupt() in the catch block that generates the attention. If this line were not there, the program would in all likelihood not work. The secret is this: If the output is only every half second, the thread is in the sleep() method almost all the time. So probably the interrupt() will just interfere with the thread sleeping. Just then sleep() is interrupted by InterruptedException, and the catch handler catches the exception. But now something unexpected happens: the interruption resets the internal flag, so isInterrupted() thinks the interruption didn't happen at all. Therefore, interrupt() must be called again because the interrupt flag must be reset and isInterrupted() can determine the end.
0