-1

How much time does a Thread need to stop/disappear after its interrupt() method has been called?

Consider the following code:

public class MyThread {
    public boolean flag = true;
    public void run() {
        while(flag) {
            doSomething();
            Thread.sleep(20);
        }
    }
}

void foo() {
    MyThread t = new MyThread();
    t.start();
    Thread.sleep(100);
    t.flag = false;
    t.interrupt();
}

Does the assignment t.flag = false; have any effect? In other words, can a thread exit its run() method and terminate "normally" before it is interrupted?

similar question

activity
  • 2,653
  • 3
  • 20
  • 44

2 Answers2

2

For sharing data one needs volatile. Better would be to catch the InterruptedException.

public class MyThread {
    public volatile boolean flag = true;
    public void run() {
        try {
            while(flag) {
                doSomething();
                Thread.sleep(20);
            }
        } catch (InterruptedException ie) {
            ...
        }
    }
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
  • I'm not trying to share any data - just wondering if setting "t.flag = false" is redundant code that has no effect. – activity Mar 15 '18 at 16:45
  • 2
    You are sharing data between threads: You have a main thread and the thread started in your main method. Both are accessing the variable ... So it must be `volatile`, exactly as this answer tells you. Besides that, catching the `InterruptedException` is the way to go, which indeed could make the `flag` variable redundant. – Seelenvirtuose Mar 15 '18 at 16:47
  • 1
    You ARE sharing data because t.flag is being modified by one thread and read by another. You don't have a try/catch around your sleep so your code won't run--therefor t.flag isn't really relevant. It's use relies heavily on how you catch that interrupt--@Joop's answer is very good at explaining this (In his implementation, for instance, t.flag is unnecessary) – Bill K Mar 15 '18 at 16:50
  • 1
    I don't think `volatile` is necessary here: https://stackoverflow.com/q/45269123/1553851 – shmosel Mar 15 '18 at 17:28
  • 1
    @shmosel In this case, `volatile` does seem to me to be necessary because `flag` can be referenced before the thread is interrupted. There's not much of a window for a race condition in the code shown, but it is there. – Andrew Henle Mar 15 '18 at 17:35
  • @AndrewHenle I guess you're right. That does seem to be the intent of the question. – shmosel Mar 15 '18 at 17:42
  • Thanks to everyone for worthwile explanation / minidiscussion which adds to the answer. – Joop Eggen Mar 16 '18 at 08:03
0

Check agains isInterrupted, anhd throw it again since when it returns true it consumes the message.

public class MyThread {
    public void run() {
        try {
            while(!Thread.isInterrupted()) {
                doSomething();
                Thread.sleep(20);
            }
        } catch (InterruptedException ie) {
            Thread.interrupt();
        }
    }
}

Making the flag unecessary.

If you want to use the flag and finish the code gracefully you don't need to use interrupt and catch the Exception.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167