5

Let's say I have a thread including while loop and I want to stop it "from outside".

public class MyThread extends Thread {

    private boolean running = true;

    @Override
    public void run() {
        while (running) {
            // do something
        }
    }

    public void setRunning(boolean running) {
        this.running = running;
   }
}

And here is the Main class:

public class Main {
    public static void main(String[] args) {
        MyThread mt = new MyThread();
        mt.start();
        // do something
        mt.setRunning(false);
    }
}

It seems to be stopping properly, but I have read that the boolean should be also volatile. Why? Will it quicken the stopping?

Wojtek
  • 1,288
  • 11
  • 16
  • @CKing et al, I'm not too sure about that dupe link. I read through the answers there as well. The question here is why, in practice, does it never actually seen to be necessary. The answers and question there dont explain it too well, or really at all, they're mostly along the lines of "you should just use it, because you're supposed to". I think this is a good distinct question. – Jason C Sep 18 '16 at 13:52
  • @JasonC It's the same question asked differently. Anyone willing to answer this question could technically add their answer to the linked duplicate as they are very closely related. The linked question is still open to new answers. The OP can comment on existing answers for more clarification. Adding a new question is laziness IMO. – Chetan Kinger Sep 18 '16 at 13:56
  • 1
    @Cking That's also very true. I suppose if this is closed (I'm still not going to dupe hammer it), the OP could ask in comments on some of those answers. It's too bad the OP of this question didn't make it clear that they read the other one, this one certainly could have been phrased in a more convincing manner. Ah well. – Jason C Sep 18 '16 at 13:57

2 Answers2

5

When Concurrent thread will cache running variable that means it will cache in thread working memory.

The volatile keyword in Java is used as an indicator to Java compiler and Thread that do not cache value of this variable and always read it from main memory. So if you want to share any variable in which read and write operation is atomic by implementation you have to declare as volatile variable.

you can have good idea in you look into the below picture enter image description here

Md Ayub Ali Sarker
  • 10,795
  • 4
  • 24
  • 19
  • Please look at my example once again and tell me if I am understanding it properly. So the `running` variable is located in main memory and when my thread is started, the `run()` function is caching it, right? So when I use `mt.setRunning(false);` in concurrent thread, it will change `running` directly in main memory and the only problem will be while checking its value in loop, because there will be delay while reading it? To sum up, is the problem in my little program SAVING or READING from the main memory? – Wojtek Sep 19 '16 at 07:48
  • Yes your are absolutely right – Md Ayub Ali Sarker Sep 19 '16 at 13:35
3

with Volatile state will be updated straight away in main memory. So as soon as running is updated , concurrent thread will see the updated state instantaneously.

Update :- It's working properly in your case probably because you don't have good number of multiple threads or if you have then by that time state has been updated in main memory by then. Best way to observe this kind of behavior to set up load tests (using tools like Jmeter). This indicates that why having good hold on theoretical/basic concepts matters otherwise you get these kind of issues on production environment which breaks your head as some projects don't have budget/timelines to do load testing

M Sach
  • 33,416
  • 76
  • 221
  • 314
  • 1
    @json c Updated the answer. – M Sach Sep 18 '16 at 13:59
  • Please look at my example once again and tell me if I am understanding it properly. So the `running` variable is located in main memory and when my thread is started, the `run()` function is caching it, right? So when I use `mt.setRunning(false);` in concurrent thread, it will change `running` directly in main memory and the only problem will be while checking its value in loop, because there will be delay while reading it? To sum up, is the problem in my little program SAVING or READING from the main memory? – Wojtek Sep 19 '16 at 07:48