且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Java中的最终静态变量是线程安全的吗?

更新时间:2022-11-23 14:20:45

sharedDatareference 是 final 的,因为它永远无法更改,因此是线程安全的.Map 的内容是 NOT 线程安全的,因为它需要***用 Guava ImmutableMap 实现或 java.util.Collections.unmodifiableMap() 或使用 java.util.concurrent 包中的 Map 实现之一.

the reference to sharedData which is final is thread safe since it can never be changed. The contents of the Map is NOT thread safe because it needs to be either wrapped with preferably a Guava ImmutableMap implementation or java.util.Collections.unmodifiableMap() or use one of the Map implementations in the java.util.concurrent package.

只有BOTH你才能在地图上获得全面的线程安全.任何包含的 Map 都必须是不可变的,或者也是并发实现之一.

Only if you do BOTH will you have comprehensive thread safety on the Map. Any contained Maps need to be immutable or one of the concurrent implementations as well.

默认情况下克隆是浅克隆,它只会返回对容器对象的引用而不是完整的副本.在普遍可用的信息中详细记录了原因.

cloning by default is a shallow clone, it will just return references to container objects not complete copies. It is well documented in generally available information on why.