Vlad
Vlad

Reputation: 21

Why isInterrupted() gives other result than Thread.currentThread().isInterrupted() (JAVA)

I study java threads and can't understand one moment: In the following code

public class ThreadsTest {
    public static void main(String[] args) throws Exception{

        ExtendsThread extendsThread = new ExtendsThread();
        Thread et = new Thread(extendsThread);
        et.start();    
        Thread.sleep(1500);
        ir.interrupt();               
    }      
}

public class ExtendsThread extends Thread {
    private Thread currentThread = Thread.currentThread();
    public void run() {
        while (!currentThread .isInterrupted()) {}
        System.out.println("ExtendsThread " + currentThread().isInterrupted());

    }
}

thread doesn't stop. But if in ExtendsThread class I check while (!Thread.currentThread().isInterrupted()) thread stops. Why private Thread currentThread = Thread.currentThread(); doesn't refer to current thread?

Upvotes: 1

Views: 174

Answers (3)

aioobe
aioobe

Reputation: 421030

Why private Thread currentThread = Thread.currentThread(); doesn't refer to current thread?

Because at the time of this variable initialization, you're on the main thread. (The variable is initialized when you do ExtendsThread extendsThread = new ExtendsThread(); and this is done from main.)


However, there are other issues with the code.

  • You're checking the interrupt on currentThread and not this thread. So start by changing the ExtendsThread code to:

    class ExtendsThread extends Thread {
        public void run() {
            while (!isInterrupted()) {
            }
            System.out.println("ExtendsThread " + isInterrupted());
        }
    }
    
  • Then you got the inheritance messed up. When you do like this:

    ExtendsThread extendsThread = new ExtendsThread();
    Thread et = new Thread(extendsThread);
    

    Which means that you're wrapping one thread within another. (So when you're interrupting you're interrupting the outer thread, but you're checking for interrupts on the inner thread.) You probably just want to get rid of the wrapping thread, and do

    ExtendsThread et = new ExtendsThread();
    

Here's the full working version of your code:

public class ThreadsTest {

    public static void main(String[] args) throws Exception{
        ExtendsThread et = new ExtendsThread();
        et.start();
        Thread.sleep(1500);
        et.interrupt();
    }
}

class ExtendsThread extends Thread {
    public void run() {
        while (!isInterrupted()) {
        }
        System.out.println("ExtendsThread " + isInterrupted());
    }
}

Upvotes: 3

piotrek
piotrek

Reputation: 14550

you should do Thread currentThread = Thread.currentThread(); inside run(). otherwise currentThread refers to main thread (the thread that constructs an object) but run is executed by different thread

Upvotes: 0

Kayaman
Kayaman

Reputation: 73558

When you create an ExtendsThread, you're assigning the thread that called the constructor to currentThread. So your currentThread isn't actually what you think it is.

Upvotes: 1

Related Questions