1

我有 3 个任务要做:

  • 首先:在同一个执行器中依次下载 htmlFile --------\ ,它工作正常。
  • 二:解压html文件---------/

  • 第三:下载图像 ------> 在另一个执行程序中,我需要下载分成 5 个文件的图像,为此我使用的是 FixedThreadPool(5),即 5 个并行下载。

但是当我点击下载时,我的图像下载开始而不等待 htmlDownload 完成,我怎么能对我的 executorPool 等待我的另一个 executor singleThread?

主类:

ExecutorService e = Executors.newSingleThreadExecutor();

            //Download HTML and Unzip Threads
            typeDownloaded = "html";
            DownloadUtil.downloadHtml(e, this, dns, port, uuid, filePathHtmlDownload, cookies, typeDownloaded);
            UnzipUtil.unZipHtml(e, this, filePathHtmlDownload, outputFolder, typeDownloaded);
            typeDownloaded = "images";
            DownloadUtil.downloadImages(e, this, dns, port, numImg, uuid, cookies, typeDownloaded);

我的 DownloadUtil 类:

public class DownloadUtil {

    private static Logger log = Logger.getLogger(LoginLocalServiceImpl.class.getName());

    public static void downloadHtml(Executor e, MainViewController controller, String dns, int port,
            String offlineUUID, String filePath, Map<String, String> cookies, String type) throws IOException {

        String urlHtml = "http://" + dns + ":" + port + Constants.TARGET_SERVICE_DOWNLOADFILES + offlineUUID;

        System.out.println(urlHtml);

        e.execute(new DownloaderTask(controller, urlHtml, filePath, cookies, type));

    }

    public static void downloadImages(Executor e, MainViewController controller, String dns, int port, int numImg,
            String uuid, Map<String, String> cookies, String type) throws SystemException, IOException {

        String filePath;
        String urlImages;

        if (numImg == 1) {
            filePath = System.getProperty("user.home") + File.separator + "Documents" + File.separator + "TargetApp" + File.separator + "TempImageDownload.zip";
            urlImages = "http://" + dns + ":" + port + Constants.TARGET_SERVICE_DOWNLOADIMAGES + uuid;
            e.execute(new DownloaderTask(controller, urlImages, filePath, cookies, type));

        } else {

            ExecutorService exec = Executors.newFixedThreadPool(numImg);

            for (int i = 0; i < numImg; i++) {
                filePath = System.getProperty("user.home") + File.separator + "Documents" + File.separator + "TargetApp" + File.separator + "TempImage" + i + "Download.zip";
                urlImages = "http://" + dns + ":" + port + Constants.TARGET_SERVICE_DOWNLOADIMAGES + uuid + "/?pos=" + (i);
                exec.execute(new DownloaderTask(controller, urlImages, filePath, cookies, type));

            }

        }


    }
}
4

2 回答 2

1

您可以延迟图片下载任务的提交,直到 html 下载完成,如下所示:

    final ExecutorService htmlDownloadExecutor = Executors.newSingleThreadExecutor();
    final ExecutorService imageDownloadExecutor = Executors.newFixedThreadPool(5);
    ...
    //Download HTML and Unzip Threads
    typeDownloaded = "html";
    final DownloaderTask htmlTask = DownloadUtil.downloadHtml(mainViewController, dns, port, uuid, filePathHtmlDownload, cookies, typeDownloaded);
    typeDownloaded = "images";
    final List<DownloaderTask> imageTasks = DownloadUtil.downloadImages(mainViewController, dns, port, numImg, uuid, cookies, typeDownloaded);

    // submit the html download escape
    htmlDownloadExecutor.execute(new Runnable() {
        @Override
        public void run() {
            // first run the html task
            htmlTask.run();
            // now submit the image tasks to run
            for (DownloaderTask imageTask : imageTasks) {
                imageDownloadExecutor.execute(imageTask);
            }

        }
    });

帮助函数已更改为返回任务实例,然后在适当的时候将任务实例提交给适当的执行器服务实例。html下载任务立即提交下载html,下载完成后提交图片下载任务。此解决方案中不涉及线程阻塞操作 (CountDownLatch.await()),因此它应该更具可扩展性/性能。

于 2013-02-07T19:01:51.693 回答
0

谷歌的 GuavaListenableFuture将在其任务完成后执行其他任务。另一种方法是使用 aCountDownLatch让图像下载线程等待,但这会在它们等待时占用这些线程。

于 2013-01-31T18:39:52.737 回答