Experimenting with the interrupt(), found that the isInterrupted() is always false.
Another thing is that replacing the isInterrupted() with Thread.interrupted() will make the thread stop.
Could someone explain why this behavior occurs?
public static void main(String[] args) {
Thread counter = new Thread(new Worker());
counter.start();
counter.interrupt();
}
static class Worker extends Thread {
@Override
public void run() {
String msg = "It was interrupted";
while (true) {
if (isInterrupted()) {
System.out.println(msg);
return;
}
}
}
}