-1

ダウンロード用と解凍用の 2 つのタスクがあります。

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 メソッドが呼び出されていることです。

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を見てください。あなたの場合に役立ちます。両方のスレッドに対して 1 つのラッチを作成し、ダウンロードが完了したら実行できcountDown()ます。解凍の最初に、次のようなものを配置できますlatch.await()。そのため、解凍はダウンロードが完了したときにのみ開始されます。

于 2013-01-29T14:45:46.647 に答える