Why my boolean variable should be volatile while stopping thread loop?
<p>Let's say I have a thread including while loop and I want to stop it "from outside".</p>

<pre><code>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;
   }
}
</code></pre>

<p>And here is the Main class:</p>

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

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