Synchronized and local copies of variables

后端 未结 4 1978

I\'m looking at some legacy code which has the following idiom:

Map myMap = someGlobalInstance.getMap();
synchronized (myMap) {
    item =         


        
4条回答
  •  南旧
    南旧 (楼主)
    2021-02-19 13:34

    I think the code could be sound, depending on what the getMap() method does. If it keeps a reference to an instance that has to be shared between threads it makes sense. The warning is irrelevant since the local variable is not initialized locally.

提交回复
热议问题