1

I have this bit of code:

  public void run()
        {
        System.out.println("ciao"); 
        try{
        throw new InterruptedException();
        }catch(InterruptedException ie){ //catch will clear the interrupted status

Thread.currentThread().interrupt();}//it will set back the interrupted status

However by following this pattern if I call the following method:

Thread1 t1 = new Thread1();//obviously Thread1 extends Thread
        t1.start();

        System.out.println(t1.isInterrupted());

The result is "false";

On the other hand if I remove the throw new InterruptedException(); and I try to interrupt it from the main method it gives back the result I expected:

Thread1 t1 = new Thread1();
        t1.start();
        t1.interrupt();
System.out.println(t1.isInterrupted());

It will return "true".

It's just a mere consistency memory problem or I am doing something wrong in the code? Thanks in advance.

EDIT: I edited the question removing the method join() as indicated by JB Nizet still the problem persists.

Rollerball
  • 12,618
  • 23
  • 92
  • 161
  • What's the code of Thread1 exactly? Is it the code in your first snippet? – JB Nizet Jun 02 '13 at 16:24
  • Was missing a final "}", edited now. Yep it just a mere test to see whether calling interrupt() from main or from within the run() method of the created thread, is the same thing or not. – Rollerball Jun 02 '13 at 16:26

2 Answers2

4

Throwing an InterrupedException doesn't set the interrupt flag. And catching it doesn't clear it.

And interrupted() is a static method, which tells if the current thread is interrupted.

Moreover, testing if a thread is interrupted after it has been joined will always return false because join() clears the interrupt flag.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • regarding "InterruptedException does not set the interrupt flag". Check this out Listing 3. Restoring the interrupted status after catching InterruptedException on this link http://www.ibm.com/developerworks/java/library/j-jtp05236/index.html – Rollerball Jun 02 '13 at 16:30
  • The convention is to clear the interrupt flag before throwing an InterruptedException, but it's only a convention, and throwing one doesn't automatically clear the flag. – JB Nizet Jun 02 '13 at 16:36
  • I mean, I am throwing an interruptException in the first snippet. Should not it set the flag as specified in the above mentioned link? – Rollerball Jun 02 '13 at 16:39
  • Throwing an InterruptedException is nod different from throwing any other exception. It doesn't set the interrupt flag. The convention is to *clear* the interrupt flag before throwing it. The convention is to let the exception propagate, or rethrow it, or reset the interrupt flag. – JB Nizet Jun 02 '13 at 16:44
1

accordin to the doc

By convention, any method that exits by throwing an InterruptedException clears interrupt status when it does so. However, it's always possible that interrupt status will immediately be set again, by another thread invoking interrupt.

And Thread.join throws InterruptedException

stinepike
  • 54,068
  • 14
  • 92
  • 112
  • Ok tnx, so what I am getting wrong in this article? Listing 3. Restoring the interrupted status after catching InterruptedException on this link ibm.com/developerworks/java/library/j-jtp05236/index.html – Rollerball Jun 02 '13 at 16:35