Java synchronized block vs. Collections.synchronizedMap

后端 未结 7 1918
天涯浪人
天涯浪人 2020-11-28 03:35

Is the following code set up to correctly synchronize the calls on synchronizedMap?

public class MyClass {
  private static Map

        
7条回答
  •  鱼传尺愫
    2020-11-28 03:52

    Collections.synchronizedMap() guarantees that each atomic operation you want to run on the map will be synchronized.

    Running two (or more) operations on the map however, must be synchronized in a block. So yes - you are synchronizing correctly.

提交回复
热议问题