Question

I read in oracle Docs for java as Thread.interrupted will bring back the Thread to non interrupted status. When a thread checks for an interrupt by invoking the static method Thread.interrupted, interrupt status is cleared. The non-static isInterrupted method, which is used by one thread to query the interrupt status of another, does not change the interrupt status flag.

Please let me know whats going wrong because ,

if(Thread.currentThread().isInterrupted())
                {
                       flag = 999;
                       Thread.currentThread().interrupted();
                       System.out.println("Interruption cleared");
                }

Above block is not called at all.

public static void main(String args[]) 
{
    int n = 5;
    int flag = 1;
    for (int i = 0; i < n; i++) 
    {

        try
        {
        System.out.println("i:"+i+":n:"+n+":Thread.currentThread().isInterrupted():"+Thread.currentThread().isInterrupted());           
        Thread.sleep(2000);
        if(i==(flag))
            Thread.currentThread().interrupt();         

        }
        catch(Exception e)
        {
            System.out.println(Thread.currentThread().isInterrupted()+"Exception :"+i);
            if(Thread.currentThread().isInterrupted())
            {
                   flag = 999;
                   Thread.currentThread().interrupted();
                   System.out.println("Interuption cleared");
            }

            System.out.println(Thread.currentThread().isInterrupted()+"Exception :"+i);
            e.printStackTrace();
        }

    }
}

Output:

i:0:n:5:Thread.currentThread().isInterrupted():false
i:1:n:5:Thread.currentThread().isInterrupted():false
i:2:n:5:Thread.currentThread().isInterrupted():true
falseException :2
falseException :2
i:3:n:5:Thread.currentThread().isInterrupted():false
java.lang.InterruptedException: sleep interrupted
    at java.lang.Thread.sleep(Native Method)
    at leo.threads.InterruptSleepMessages.main(InterruptSleepMessages.java:14)
i:4:n:5:Thread.currentThread().isInterrupted():false
Was it helpful?

Solution

What I said in the comment was partly wrong. Actually Java 7 Docs sais:

If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared and it will receive an InterruptedException.

Source: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#interrupt()

So, the InterruptedException being thrown clears the status, not catching it. Seen effect is the same. When i=1 the flag is set by interrupt() and executing sleep will lead to the flag being cleared and the exception thrown which leads to seen output.

Also see: http://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#sleep(long)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top