volatile关键字如何确保对象的字段对其他线程可见?

时间:2018-09-23 21:17:07

标签: java concurrency 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;
    }
}

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

1 个答案:

答案 0 :(得分:2)

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

我将打印语句添加到您的getters / setters中以记录通话时间:-

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
.
.
.

您关注的问题是以下行:- Thread-0 5 is red 它应该已经打印了black,对吧?

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

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

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

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