我正在创建一个Java应用程序。启动时,我的应用程序将下载所有必需的文件。我的应用程序将解析XML文件,并从XML文件中从URL下载文件。我希望我的应用程序下载文件“一步一步”,所以我使用FutureTask --我的问题是,FutureTask不适合我的应用程序。
这是我代码的一部分。
Startup.class
public void startDownloading()
{
Thread t = new Thread(new Runnable()
{
public void run()
{
downloader.startDownload();
}
});
t.run();
}
}Downloader.class
private LibrariesDownloader ld;
private RDownloader rd;
public Downloader()
{
this.ld = new LibrariesDownloader(launcher);
this.rd = new RDownloader(launcher);
}
public void startDownload()
{
ExecutorService executor = Executors.newFixedThreadPool(2);
FutureTask<Void> libDownloader = new FutureTask<Void>(ld);
FutureTask<Void> resDownloader = new FutureTask<Void>(rd);
executor.execute(libDownloader);
if(libDownloader.isDone())
{
executor.execute(resDownloader);
}
}LibrariesDownloader.class(& RDownloader.class(代码几乎相同,只有URL不同))
public class LibrariesDownloader implements Callable<Void>
{
private Proxy proxy = Proxy.NO_PROXY;
@Override
public Void call() throws Exception
{
try
{
URL resourceUrl = new URL("http://www.exmaple.com/libraries.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(resourceUrl.openConnection(proxy).getInputStream());
NodeList nodeLst = doc.getElementsByTagName("Content");
for (int i = 0; i < nodeLst.getLength(); i++)
{
Node node = nodeLst.item(i);
if (node.getNodeType() == 1)
{
Element element = (Element)node;
String key = element.getElementsByTagName("Key").item(0).getChildNodes().item(0).getNodeValue();
final File path = new File("C://test/", key);
final String url = "http://www.exmaple.com/dl/" + key;
final String fileName = key;
SwingWorker<Void, Void> worker = new SwingWorker<Void, Void>()
{
@Override
protected Void doInBackground() throws Exception
{
try
{
URL fileURL = new URL(url);
org.apache.commons.io.FileUtils.copyURLToFile(fileURL, path);
}
catch(Exception e)
{
URL redownloadURL = new URL("http://www.example.com/dl/" + fileName);
File p = new File("C://test/", fileName);
org.apache.commons.io.FileUtils.copyURLToFile(redownloadURL, p);
}
return null;
}
@Override
public void done()
{
System.out.println(fileName + " had downloaded successfully");
}
};
worker.execute();
}
}
}
catch(Exception e)
{
launcher.println("An error was found when trying to download libraries file " + e);
}
return null;
}
}我的XML文件中有大量的<Key></Key>。我的应用程序可以执行LibrariesDownloader并下载所有库文件。在下载了所有库文件之后,我的应用程序就到此为止了。它不会执行RDownloader。
我的应用程序中有错误的代码吗?谢谢你帮我。
发布于 2013-10-05 07:08:15
启动一个新线程。
t.run();
应该是t.start()。线程调度程序调用run()。
您可能需要一个繁忙/等待循环或LibrariesDownloader的超时
if(libDownloader.isDone())
{
executor.execute(resDownloader);
}应该是
Future<?> future = executor.submit(libDownloader);
while (!future.isDone()) {
//bad spin wait
}
executor.execute(resDownloader);更好的是,使用Executors.newSingleThreadExecutor()或更健壮的Executors.newFixedThreadPool(1)创建一个单独的Executors.newFixedThreadPool(1),并同时提交它们。第二项任务将排队。
代码片段看起来就像
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(libDownloader);
executor.execute(resDownloader);https://stackoverflow.com/questions/19194847
复制相似问题