我有一个web应用程序需要有时从url下载一些字节并将其打包并发送回请求者。下载的字节存储了一段时间,以便在需要下载相同的URL时可以重新使用它们。我试图找出如何最好地防止线程同时下载同一个网址,如果请求同时进入。我正在考虑创建一个如下所示的类,以防止同时下载相同的URL。如果一个url无法被锁定,那么它会等待,直到它不再被锁定,只要它在解锁后不存在,就尝试下载它。在java中创建安全的多线程文件
public class URLDownloader
{
HashMap<String,String> activeThreads = new HashMap<String,String>();
public synchronized void lockURL(String url, String threadID) throws UnableToLockURLException
{
if(!activeThreads.containsKey(url))
activeThreads.put(url, threadID)
else
throw UnableToLockURLException()
}
public synchonized void unlockURL(String url, String threadID)
{
//need to check to make sure its locked and by the passed in thread
returns activeThreads.remove(url);
}
public synchonized void isURLStillLocked(String url)
{
returns activeThreads.contains(url);
}
}
有没有人有更好的解决方案呢?我的解决方案似乎有效吗?是否有任何开源组件已经做得非常好,我可以利用?
感谢