Not thread-safe Object publishing

前端 未结 7 2107
眼角桃花
眼角桃花 2020-12-02 21:44

Reading \"Java Concurrency In Practice\", there\'s this part in section 3.5:

public Holder holder;
public void initialize() {
     holder = new Holder(42);
}         


        
7条回答
  •  醉梦人生
    2020-12-02 22:00

    The basic issue is that without proper synchronization, how writes to memory may manifest in different threads. The classic example:

    a = 1;
    b = 2;
    

    If you do that on one thread, a second thread may see b set to 2 before a is set to 1. Furthermore, it's possible for there to be an unbounded amount of time between a second thread seeing one of those variables get updated and the other variable being updated.

提交回复
热议问题