简体   繁体   English

核心Java线程和易失性关键字用法

[英]Core Java Threading and Volatile keyword Usage

I have recently started studying multithreading in core Java. 我最近开始研究核心Java中的多线程。 I am studying a Volatile keyword. 我正在研究一个Volatile关键字。 As per my understanding threads are allowed to keep the values of the variables locally. 根据我的理解,允许线程在本地保存变量的值。 If the variables are driving any logic (eg loop) then the changes to the variables will not be visible to the threads and they will keep using the outdated cached values unless the variable declared as 'volatile'. 如果变量驱动任何逻辑(例如循环),则线程将看不到对变量的更改,并且除非将变量声明为“volatile”,否则它们将继续使用过时的缓存值。

I created a small code to demonstrate this, however, my observation is different. 我创建了一个小代码来演示这个,但是,我的观察是不同的。 The changes made by one thread (main thread) are very well visible to the other thread even if the shared variable 'done' is not volatile. 即使共享变量“done”不是volatile,一个线程(主线程)所做的更改对另一个线程也是非常明显的。 Could anyone please help me understand this behavior 谁能帮助我理解这种行为

package com.test;

public class App implements Runnable {
    private boolean done= true;
    private static App a = new App();   


    public static void main( String[] args ) throws InterruptedException {
        Thread t = new Thread(a);
        t.start();
        System.out.println("Main Thread:"+ Thread.currentThread().getName());
        Thread.sleep(1);
        a.done=false;
        System.out.println("Value of done in main method is:"+ a.done);
    }

    public void run() {
        while(done) {
             System.out.println("Second Thread:"+ Thread.currentThread().getName());
            System.out.println("Still running");
        }
    }
}

Output of the above code is as follows 上述代码的输出如下

Main Thread:main
Second Thread:Thread-0
Still running
Second Thread:Thread-0
Still running
Second Thread:Thread-0
Still running
Second Thread:Thread-0
Still running
Second Thread:Thread-0
Still running
Second Thread:Thread-0
Still running
Value of done in main method is:false

The keyword volatile guarantees the behavior you described. 关键字volatile保证您描述的行为。 The value of a volatile field is visible to all readers after a write operation. write操作之后,所有读取器都可以看到volatile字段的值。 This means that no cached values are used. 这意味着不使用缓存值。

If you are not using the volatile keyword this does not automatically mean though that you are always using old, cached variables. 如果您不使用volatile关键字,这并不意味着您始终使用旧的缓存变量。 It's just the possibility that this could happen. 可能发生这种情况。


Takeaway : 外卖

You use the volatile keyword to guarantee memory visibility. 您可以使用volatile关键字来保证内存可见性。 It does not mean that not using it will break your code as soon as you use multithreading. 这并不意味着不使用它会在您使用多线程时立即破坏您的代码。


Also note that volatile does not guarantee atomic interaction with your variable. 另请注意, volatile不保证与变量的原子交互。 This means that a volatile field is not automatically thread safe regarding race conditions and other potential trouble. 这意味着对于竞争条件和其他潜在问题, volatile字段不会自动保证线程安全。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM