首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java - FutureTask不工作吗?

Java - FutureTask不工作吗?
EN

Stack Overflow用户
提问于 2013-10-05 06:42:13
回答 1查看 1.3K关注 0票数 0

我正在创建一个Java应用程序。启动时,我的应用程序将下载所有必需的文件。我的应用程序将解析XML文件,并从XML文件中从URL下载文件。我希望我的应用程序下载文件“一步一步”,所以我使用FutureTask --我的问题是,FutureTask不适合我的应用程序。

这是我代码的一部分。

Startup.class

代码语言:javascript
复制
public void startDownloading()
{

    Thread t = new Thread(new Runnable()
    {
        public void run()
        {
            downloader.startDownload();
        }
    });
    t.run();
    }
}

Downloader.class

代码语言:javascript
复制
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不同))

代码语言:javascript
复制
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

我的应用程序中有错误的代码吗?谢谢你帮我。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-05 07:08:15

启动一个新线程。

t.run();

应该是t.start()。线程调度程序调用run()

您可能需要一个繁忙/等待循环或LibrariesDownloader的超时

代码语言:javascript
复制
if(libDownloader.isDone())
    {
        executor.execute(resDownloader);
    }

应该是

代码语言:javascript
复制
Future<?> future = executor.submit(libDownloader);

while (!future.isDone()) {
    //bad spin wait
} 

executor.execute(resDownloader);

更好的是,使用Executors.newSingleThreadExecutor()或更健壮的Executors.newFixedThreadPool(1)创建一个单独的Executors.newFixedThreadPool(1),并同时提交它们。第二项任务将排队。

代码片段看起来就像

代码语言:javascript
复制
ExecutorService executor = Executors.newFixedThreadPool(1);
executor.execute(libDownloader);
executor.execute(resDownloader);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19194847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档