0

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?

tin_tin
  • 478
  • 3
  • 10
  • 24
  • Does the t thread ever finish or does it just keep spinning? Probably the interruption happens before t has a chance to be alive, so it doesn't work. – Nathan Hughes Jan 20 '22 at 13:22
  • Thread t gets chance to run. When i run this code it prints "im interrupted::true" from thread t's run method. I just wanted to know how will the worker thread in threadpool know of the task it is running has been interrupted? – tin_tin Jan 20 '22 at 13:26
  • 1
    There is no threadpool involved here. The started thread is interrupted immediately and finishes. The interrupted flag doesn't work on a dead thread. Pools are written so that interrupting a thread doesn't stop the thread. – Nathan Hughes Jan 20 '22 at 13:29
  • Threadpool is not involved here. But how will threadpool in general know if the task has been interrupted. Brian Goetz says that task must always restore interrupt status. Now who will read that status, how it will be read and who will act on that interrupt? – tin_tin Jan 20 '22 at 13:31
  • 1
    @NathanHughes thanks for the following comment "Pools are written so that interrupting a thread doesn't stop the thread." Now I have understood what interruption is and how it works in general and in threadpool. – tin_tin Jan 20 '22 at 13:41
  • 1
    The use of interruption is for the one thread to notify another that it is interrupted. If you want the main thread to know if the `InterruptedThread` was interrupted, then I would save the interrupt info in a `boolean` field and after calling `t.join()` you can read that field. – Gray Jan 21 '22 at 22:30
  • 1
    @Gray Brian Goetz says that the thread interrupted should do Thread.currentThread().interrupt to reassert the fact it was interrupted so that the calling thread can read that status and do something useful. So i was just testing that functionality to understand how worker thread are interrupted in threadpool – tin_tin Jan 24 '22 at 17:47
  • Brain Goetz is right @tin_tin especially when we catch `InterruptedException` you should immediately re-interrupt the current thread so that callers will be able to see the interrupt status of the current thread. – Gray Jan 25 '22 at 17:49
  • @Gray That was not happening in the example code i pasted above. The main thread starts InterruptedThread and then interrupts it. InterruptedThread reasserts the interrupt status in catch block but still when i checked the status of interruption in main it returned false. Please look at the code above to suggest something as to how main can check if InterruptedThread was interrupted. – tin_tin Jan 26 '22 at 15:21
  • That was just a point of information @tin_tin. I suggested above about how to test if a thread had been interrupted. The thread should check _itself_ and set a field that you can test from the main thread. – Gray Jan 27 '22 at 18:37

0 Answers0