discontinued method of Thread class in Java

advertisements

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.


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.