0
votes

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
1
You have written a terribly convoluted piece of code, as if your goal is to confuse yourself. Why not try something dead-simple? currentThread().interrupt(); System.out.println(Thread.interrupted()); System.out.println(Thread.interrupted()); - Marko Topolnik
Remember: Catching an InterruptedException also clears the status. You catch Exception, therefore also InterruptedException thrown by Thread.sleep when i=2. Then the status is cleared by catch and your if condition is false. So it does work perfectly. - Fildor
@Fildor ok I understood that Exception also clears interrupted status of the Thread.Thanks for you Explaination.It was useful. - sunleo
You might want to accept the answer on this? - Fildor

1 Answers

1
votes

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)