Java 多线程原子引用赋值
2022-09-02 12:40:21
我有一个缓存,我使用简单的HashMap实现。喜欢 -
HashMap<String,String> cache = new HashMap<String,String>();
此缓存大部分时间都用于从中读取值。我有另一种方法来重新加载缓存,在此方法内部,我基本上创建了一个新的缓存,然后分配引用。据我所知,对象引用的赋值在Java中是原子的。
public class myClass {
private HashMap<String,String> cache = null;
public void init() {
refreshCache();
}
// this method can be called occasionally to update the cache.
public void refreshCache() {
HashMap<String,String> newcache = new HashMap<String,String>();
// code to fill up the new cache
// and then finally
cache = newcache; //assign the old cache to the new one in Atomic way
}
}
我知道,如果我不将缓存声明为易失性,其他线程将无法看到更改,但对于我的用例来说,将缓存中的更改传播到其他线程并不是时间紧迫的,他们可以继续长时间使用旧缓存。
是否看到任何线程问题?考虑到许多线程正在从缓存中读取,并且仅在重新加载缓存时。
编辑 - 我的主要困惑是我在这里不必使用原子引用,因为赋值操作本身是原子的?
编辑 - 我明白,为了使排序正确,我应该将缓存标记为易失性。但是,如果 refreshCache 方法被标记为已同步,我不必将缓存设置为易失性,因为同步块将负责排序和可见性?