Volatile piggyback. Is this enough for visiblity?

后端 未结 5 885
梦谈多话
梦谈多话 2020-12-05 00:45

This is about volatile piggyback. Purpose: I want to reach a lightweight vars visibilty. Consistency of a_b_c is not important. I have a bunch of vars and I don\'t want to m

5条回答
  •  春和景丽
    2020-12-05 01:39

    The pattern is usually like this.

    public void setup() {
        a = 2;
        b = 3;
        c = 4;
        sync();
    }
    

    However, while this guarantees the other threads will see this change, the other threads can see an incomplete change. e.g. the Thread2 might see a = 2, b = 3, c = 0. or even possibly a = 2, b = 0, c = 4;

    Using the sync() on the reader doesn't help much.

提交回复
热议问题