volatileキーワードの2つの意味


        (      、        )  volatile     ,          :

1)                     ,              ,                。
(              ,                   )

2)         。

  :      ,        ,          juc    , :synchronized,lock,AtomicBoolean 

volatitleクラシックケース:
package test;

public class RunThread extends Thread {
    /** volatile */
    private /*volatile*/ boolean isRunning = true;

    private void setRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    public void run() {
        System.out.println(Thread.currentThread().getName()+" > start!!!    run()    ...");
        while (isRunning) {
        }
        System.out.println(Thread.currentThread().getName()+"> stop!!!      ..."+isRunning);
    }

    public static void main(String[] args) throws InterruptedException {
        RunThread myThread = new RunThread();
        myThread.start();
        Thread.sleep(3000);
        myThread.setRunning(false);
        System.out.println(Thread.currentThread().getName()+"> isRunning          false");
    }
}
#  volatile   :
Thread-0 > start!!!    run()    ...
main> isRunning          false

# volatile   :
Thread-0 > start!!!    run()    ...
main> isRunning          false
Thread-0> stop!!!      ...false