Im testing interruption with below two methods. Main thread interrupts the other thread and other thread just returns. But when i check the interrupt status in main it always returns false. Brian Goetz says that owner thread can interrupt a thread and the owner thread can act on interruption. If the thread interruption cannot be known in main then how would main thread act on interruption of other thread.
public class InterruptedThread implements Runnable{
@Override
public void run() {
while(true) {
if(Thread.currentThread().isInterrupted()) {
System.out.println("im interrupted::"+Thread.currentThread().isInterrupted());
Thread.currentThread().interrupt();
return;
}
}
}
}
public static void main(String [] args) throws Exception{
Thread t = new Thread(new InterruptedThread());
t.start();
t.interrupt();
Thread.sleep(500);
System.out.println(t.isInterrupted()); // 1
}
//1 line marked as 1 always returns false. Then what is the use of interruption?
Edit Question: The main reason behind this question was to know how the threads in threadpool will detect if the task it is running has been interrupted? I was thinking that worker thread will poll the running task's interrupt status to know whether the task was interrupted?