Is reference update thread safe?

后端 未结 6 520
野趣味
野趣味 2021-01-26 13:49
public class Test{
   private MyObj myobj = new MyObj(); //it is not volatile


   public class Updater extends Thred{
      myobje = getNewObjFromDb() ; //not am settin         


        
6条回答
  •  不知归路
    2021-01-26 14:24

    No, it isn't.

    Without volatile, calling getData() from a different thread may return a stale cached value.
    volatile forces assignments from one thread to be visible on all other threads immediately.

    Note that if the object itself is not immutable, you are likely to have other problems.

提交回复
热议问题