我想在ConcurrentHashMap
. 对二进制文件的修改也应该反映在缓存中。由于缓存将被多个线程使用,所有 IO 操作都是同步的。地图的修改进入同synchronized
一块内。大致如下:
synchronized (file) {
file.deleteRecord(index)
map.remove(index);
}
和
synchronized(file) {
file.writeRecord(index, record);
map.put(index, record);
}
两者map
和file
都是私有的,从缓存类外部看不到。
map.get(index)
如果缓存读取,即没有块,是否保留线程安全synchronized
?
正如我前面提到的,ConcurrentHashMap
是用作地图实现的。