java - do I need a lock? -


myobject myobj ...  public void updateobj(){      myobject newobj = getnewmyobject();      myobj = newobj; }  public int getsomething(){      //o(n^2) operation performed in getsomething method      int = myobj.getsomething();      return something; } 

suppose main thread periodically calls updateobj() , child thread calling getsomething() method pretty often.

do need lock (or declare methods synchronized) before myobj = newobj; , int = myobj.getsomething();

someone argued don't need lock here because in java, assignment operations (e.g myobj = newobj;) atomic. don't myobj.getsomething(); not atomic operation o(n^2) think locking still needed. correct?

thanks in advance.

you need declare myobject volatile, otherwise getsomething() method may not see updated object. other cannot see synchronization issues in above code


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -