我有 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));
}
}
}
}