Here's a class Worker that extends Thread.
class Worker extends Thread {
@Override
public void run() {
String msg = "It was interrupted"; // 1
try {
Thread.sleep(200); // 2
} catch (InterruptedException e) {
System.out.println(msg);
}
int i = 0; // 3
while (i < 100_000) {
if (isInterrupted()) {
System.out.println(msg);
return;
}
i++; // 4
}
System.out.println("Finished"); // 5
}
}
Another thread calls the interrupt method of this thread. The Worker thread will print "It was interrupted" if another thread interrupts it during...