Java-FutureTask不工作?
我正在创建一个Java应用程序。启动时,我的应用程序将下载所有必需的文件。我的应用程序将解析XML文件,并从XML文件的URL下载文件。我希望我的应用程序“一步一步”下载文件,所以我使用Java-FutureTask不工作?,java,Java,我正在创建一个Java应用程序。启动时,我的应用程序将下载所有必需的文件。我的应用程序将解析XML文件,并从XML文件的URL下载文件。我希望我的应用程序“一步一步”下载文件,所以我使用FutureTask我的问题是,FutureTask不适用于我的应用程序 这是我代码的一部分 Startup.class public void startDownloading() { Thread t = new Thread(new Runnable() { public
FutureTask
我的问题是,FutureTask
不适用于我的应用程序
这是我代码的一部分
Startup.class
public void startDownloading()
{
Thread t = new Thread(new Runnable()
{
public void run()
{
downloader.startDownload();
}
});
t.run();
}
}
下载程序.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);
}
}
私人图书馆下载器ld;
私家路;;
公共下载程序()
{
this.ld=新库下载器(启动器);
this.rd=新的RDownloader(启动器);
}
public void startDownload()
{
ExecutorService executor=Executors.newFixedThreadPool(2);
FutureTask libDownloader=新的FutureTask(ld);
FutureTask resDownloader=新的FutureTask(rd);
executor.execute(libDownloader);
if(libDownloader.isDone())
{
executor.execute(resDownloader);
}
}
LibrariesDownloader.class(&RDownloader.class)(代码几乎相同,只是URL不同))
公共类LibrariesDownloader实现可调用
{
private Proxy Proxy=Proxy.NO_Proxy;
@凌驾
public Void call()引发异常
{
尝试
{
URL resourceUrl=新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(“内容”);
对于(int i=0;i
在我的XML文件中有大量的
。我的应用程序可以执行LibrariesDownloader
并下载所有库文件。下载完所有库文件后,我的应用程序就停止了。它不会执行RDownloader
我的申请表中有错误代码吗?谢谢你的帮助。你可以用 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);
定义“不工作”。另外,您应该阅读Thread.run()、Thread.start()和FutureTask.isDone()的javadoc。仅仅从代码上看,还不清楚您想做什么。我想在LibrariesDownloader.class下载文件后,RDownloader将启动并下载“R”文件,因此您希望顺序执行而不是并行执行。那么为什么要使用线程呢?正如我所说,阅读isDone()的javadoc。这不是一种阻塞方法。get()是一个阻塞方法。我以前读过FutureTask的所有javadoc。我的代码不起作用。。你能帮我一下吗?plzSo,你在哪里读到isDone()会等到任务完成后再返回true或false?为什么不重新阅读get()方法的javadoc呢?不,我想在下载完
LibrariesDownloader
后,RDownloader
将启动并下载另一个文件列表again@user2718549这就是将要发生的事情。除非LibrariesDownloader
完成,RDownloader
不会启动。你试过代码吗?我试过所有的代码,它们都不起作用,它将同时启动LibrariesDownloader
和RDownloader
。@user2718549你应该执行worker.get()
在worker.execute()之后执行SwingWorker
在哪里使用worker.get()
?当LibrariesDownloader
和RDownloader
下载文件时,我使用SwingWorker
,因为我的GUI将“冻结”,所以我必须在后台运行下载过程
Future<?> future = executor.submit(libDownloader);
while (!future.isDone()) {
//bad spin wait
}
executor.execute(resDownloader);
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(libDownloader);
executor.execute(resDownloader);