-1

我有两个任务,一个用于下载,另一个用于解压缩;

public class DownloadUtil {

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

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

        System.out.println(urlHtml);

        Executors.newSingleThreadExecutor().execute(new DownloaderTask(controller, urlHtml, filePath, cookies));
    }

public class UnzipUtil {

    public static void unZipIt(String zipFile, String outputFolder) {

        Executors.newSingleThreadExecutor().execute(new UnzipTask(zipFile, outputFolder));
    }
}

我在我的代码中这样称呼它们:

DownloadUtil.downloadHtml(this, dns, port, uuid, filePathHtmlDownload, cookies);
UnzipUtil.unZipIt(filePathHtmlDownload, outputFolder);

但问题是 Unzip 方法在下载方法完成之前调用,我该怎么做才能 unZipIt 等待 downloadHtml 方法?

4

2 回答 2

4

将相同的内容传递SingleThreadExecutor给两种方法。然后,您的任务将由Executor.

Executor e = Executors.newSingleThreadExecutor();
DownloadUtil.downloadHtml(e, this, dns, port, uuid, filePathHtmlDownload, cookies);
UnzipUtil.unZipIt(e, filePathHtmlDownload, outputFolder);

你的方法现在看起来像:

public class DownloadUtil {

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

    ...

    e.execute(new DownloaderTask(controller, urlHtml, filePath, cookies));
}
于 2013-01-29T14:49:33.270 回答
-1

看看CountDownLatch。这将对您的情况有所帮助。您可以为两个线程创建一个闩锁,下载完成后您可以执行countDown()。在解压缩的开头,您可以放置​​类似latch.await(). 因此,只有在下载完成后才会开始解压缩。

于 2013-01-29T14:45:46.647 回答