1 java concurrency synchronization concurrenthashmap
我想借助于缓存一些IO ConcurrentHashMap.二进制文件的修改也应该反映在缓存中.由于缓存将由多个线程使用,因此所有IO操作都是同步的.地图的修改进入同synchronized一块内.大致如下:
synchronized (file) {
file.deleteRecord(index)
map.remove(index);
}
Run Code Online (Sandbox Code Playgroud)
和
synchronized(file) {
file.writeRecord(index, record);
map.put(index, record);
}
Run Code Online (Sandbox Code Playgroud)
这两个map和file是私人,而不是从缓存级外看到.
如果缓存读取,是否保持线程安全,即map.get(index)没有synchronized块?
正如我前面提到的,ConcurrentHashMap用作地图实现.