我正在尝试从数据库同步硬盘上的文件兑现。
我正在检查文件是否存在,如果不存在,我从数据库中获取文件并进行部署。由于竞争条件,我不想多次写文件。
以下是我制作的代码:
IMPORTENT :此代码位于Bean内,具有所有含义
@Override public String getThumbnailPictureUrl(Design design) { String relativePath = String.format(THUMBNAIL_URL, design.getId(), design.getThumbnailPicture().getFileName()); String realPath = servletContext.getRealPath("/"+relativePath); logger.info("Request Thumbnail picture for design: " + design.getId()); logger.info("Thumbnail picture relative path: " + relativePath); logger.info("Thumbnail picture real path: " + realPath); File file = new File(realPath); if(!file.exists()) { synchronized (thumbnailLock) { if(!file.exists()) { logger.warn("Could not fild file in path: " + realPath); FileAttachment pictureAttachment = design.getThumbnailPicture(); Hibernate.initialize(pictureAttachment.getAttachment()); Data data = (Data) pictureAttachment.getAttachment(); file = toolBox.convertBlobToFile(data.getBlob(), file); logger.warn("file created in path: " + realPath); } } } return relativePath; }
使用此解决方案以防万一我找不到文件我真的不会写文件2次以及任何其他文件,因为我正在同步整个块尝试到达它的所有线程,即使是要写一个不同的文件。
有什么建议吗?
感谢。
答案 0 :(得分:5)
与@ Avi的解决方案类似,但使用ConcurrentHashMap。
private final ConcurrentMap<String, Object> map = new ConcurrentHashMap<>();
String name = file.getName();
Object lock = map.get(name);
if (lock == null) {
map.putIfAbsent(name, new Object());
lock = map.get(name);
}
synchronized (lock) {
// do something
}
map.remove(name);
答案 1 :(得分:1)
如何创建HashMap
锁?键将是文件路径,值将只是用作锁的对象。假设这张地图定义如下:
Map<String, Object> locks = new Map<String, Object>();
这是用法:
if(!file.exists())
{
Object lock = null;
synchronized (locks) {
lock = locks.get(file.getName());
if(lock == null) {
lock = new Object();
locks.put(file.getName(), lock);
}
}
synchronized (lock)
{
if(!file.exists())
{
logger.warn("Could not fild file in path: " + realPath);
FileAttachment pictureAttachment = design.getThumbnailPicture();
Hibernate.initialize(pictureAttachment.getAttachment());
Data data = (Data) pictureAttachment.getAttachment();
file = toolBox.convertBlobToFile(data.getBlob(), file);
logger.warn("file created in path: " + realPath);
}
}
synchronized(locks) {
map.remove(lock));
}
}