Thread t1 =new Thread( () -> {
map.put("a",new AtomicLong(0));
for (int i =0;i<100000;i++){
map.get("a").incrementAndGet();
}
});
t1.start();
Thread t2 =new Thread( () -> {
Map copyMap =new HashMap<>();
copyMap.putAll(map);
while (true){
for(Map.Entry entry : copyMap.entrySet()){
System.out.println(entry.getValue());
}
}
});
t2.start();
以上代码中,虽然copymap对我们的静态map值进行了一次putall,(map是静态的ConcurrentHashMap
Map拷贝 关于对象深拷贝 浅拷贝的问题 - chenxiangxiang - 博客园