使用CAS的方法
AtomicReference是在每次批量更新时复制地图内容。
AtomicReference<Map<String, String>> workingMapRef = new AtomicReference<>(new HashMap<>());
该映射可以是并发的,但是对于“批量更新”它是只读的。然后
updateState循环执行,
doUpdateState()直到得到正确的结果,这意味着您的值已更新。
void updateState() { while (!doUpdateState());}boolean doUpdateState() { Map<String, String> workingMap = workingMapRef.get(); //copy map content Map<String, String> newState = new HashMap<>(workingMap); //you can make it concurrent newState.put("b", "b1"); newState.put("c", "c2"); newState.put("d", "d1"); return workingMapRef.compareAndSet(workingMap, newState);}


