volatile关键字如何确保对象的字段对其他线程可见? [英] How does volatile keyword ensure an object`s fields are visible to other threads?

查看:130
本文介绍了volatile关键字如何确保对象的字段对其他线程可见?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public class ObjectPropertiesVolatileTest {

    static Cup cup = new Cup();

    public static void changeColor() {
        cup.setColor("black"); // change color of cup to black
    }

    public static void main(String[] args) {
        cup.setColor("red"); // initialize color of cup as red

        for(int i = 0; i < 3; i++) {
            new Thread(){
                public void run() {
                    for(int i = 0; i < 10; i++) 
                        System.out.println(Thread.currentThread().getName()
                                + " " + i + " is " + cup.getColor());

                    if(Thread.currentThread().getName().equals("Thread-1"))
                        changeColor(); // Thread-1 changes the color of cup

                    for(int i = 0; i < 10; i++) 
                        System.out.println(Thread.currentThread().getName()
                                + " " +i + " is " + cup.getColor());

                }
            }.start();
        }

    }

}

class Cup {

    private volatile String color;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }
}

我的问题是:如何确保字段<$仅使用 volatile 关键字而不是使用同步或锁定,才能使对象杯的c $ c> color 对其他线程可见?

My question is: how to ensure the field color of the object cup is visible to other threads only using volatile keyword instead of using synchronization or lock?

推荐答案

我认为它工作正常。问题是打印,调用打印语句,合并 System.out.println(Thread.currentThread()。getName()+ + i +中的字符串是 + cup.getColor ()); 等未同步。

I believe it is working fine. The problem is that the printing, call to print statement, merging of strings in System.out.println(Thread.currentThread().getName() + " " +i + " is " + cup.getColor());, etc. are not synchronised.

我在您的getter / setter中添加了打印语句以记录通话时间:-

I added print statements to your getters/setters to log the time of calls:-

public String getColor() {
    System.out.println("-------------------------get " + Thread.currentThread().getName() + this.color + " " + System.nanoTime());
    return color;
}

public void setColor(String color) {
    System.out.println("-------------------------set " + Thread.currentThread().getName() + color + this.color + " " + System.nanoTime());
    this.color = color;
}

打印内容的摘要:-

.
.
.
Thread-0 4 is red
-------------------------get Thread-1red 1356826035808750
Thread-1 8 is red
-------------------------get Thread-2red 1356826035425706
Thread-2 2 is red
-------------------------get Thread-1red 1356826035963697
Thread-1 9 is red
-------------------------get Thread-0red 1356826035894581
-------------------------set Thread-1blackred 1356826036165653
-------------------------get Thread-2red 1356826036036858
Thread-2 3 is black
-------------------------get Thread-1black 1356826036219728
Thread-1 0 is black
Thread-0 5 is red
-------------------------get Thread-1black 1356826036402925
Thread-1 1 is black
-------------------------get Thread-2black 1356826036305139
Thread-2 4 is black
-------------------------get Thread-1black 1356826036535236
Thread-1 2 is black
.
.
.

您关注的问题是以下行:-
线程-0 5是红色的
它应该已经打印了黑色,对吗?

The problem you are concerned about is this line:- Thread-0 5 is red It should have printed black, right?

好吧,这个线程在另一个线程更改值之前进入了吸气剂。从以下几行可以明显看出这一点:-

Well, this thread entered the getter before the other thread changed the value. This is evident from these lines:-

-------------------------get Thread-0red 1356826035894581
-------------------------set Thread-1blackred 1356826036165653

因此它可以正确读取该值,但是此后的处理与其他线程所花费的时间相比花费了很长时间

So it read the value correctly but the processing done after that took a long time as compared to the time taken by other threads in doing what they were doing.

很高兴有人纠正我,如果我错了。 :)

Happy for someone to correct me if I am wrong. :)

这篇关于volatile关键字如何确保对象的字段对其他线程可见?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆