Java 如何让一个执行器FixedThreadPool等待另一个执行器SingleThreadExecutor?

Java 如何让一个执行器FixedThreadPool等待另一个执行器SingleThreadExecutor?,java,multithreading,executorservice,executors,Java,Multithreading,Executorservice,Executors,我有3项任务要做: 首先:按顺序下载htmlFile-----------\n这两个文件都在同一个执行器中,工作正常 第二:解压HTML文件----------------/ 第三:下载图像------->在另一个执行器中,我需要下载分为5个文件的图像,为此我使用FixedThreadPool(5),换句话说,5个并行下载 但当我点击下载时,我的图像下载开始而不等待htmlDownload完成,我如何处理我的executorPool等待我的另一个executor singleThread 主

我有3项任务要做:

  • 首先:按顺序下载htmlFile-----------\n这两个文件都在同一个执行器中,工作正常
  • 第二:解压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));

            }

        }


    }
}
公共类下载util{
私有静态记录器log=Logger.getLogger(loginlocalserviceinpl.class.getName());
公共静态无效下载HTML(执行器e、MainViewController控制器、字符串dns、int端口、,
字符串offlineUUID、字符串文件路径、映射cookies、字符串类型)引发IOException{
字符串urlHtml=“http://”+dns+:“+port+Constants.TARGET\u SERVICE\u DOWNLOADFILES+offlineUUID;
System.out.println(urlHtml);
e、 执行(新的下载任务(控制器、URL HTML、文件路径、cookies、类型));
}
公共静态void下载映像(Executor e、MainViewController控制器、字符串dns、int-port、int-numig、,
字符串uuid、映射cookies、字符串类型)引发SystemException、IOException{
字符串文件路径;
字符串图像;
如果(numImg==1){
filePath=System.getProperty(“user.home”)+File.separator+“Documents”+File.separator+“TargetApp”+File.separator+“TempImageDownload.zip”;
urlmages=“http://”+dns+:“+port+Constants.TARGET\u SERVICE\u DOWNLOADIMAGES+uuid;
e、 执行(新的下载任务(控制器、URL图像、文件路径、cookies、类型));
}否则{
ExecutorService exec=Executors.newFixedThreadPool(numImg);
对于(int i=0;i
谷歌的番石榴(Guava)拥有
ListenableFuture
,在完成任务后将执行其他任务。另一种方法是使用
倒计时闩锁
让图像下载线程等待,但这会在线程等待时占用这些线程

谷歌的番石榴有
ListenableFuture
,它将在完成任务后执行其他任务。另一种方法是使用
倒计时闩锁
让图像下载线程等待,但这会在线程等待时占用这些线程

您可以延迟图像下载任务的提交,直到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);
            }

        }
    });
final ExecutorService htmlDownloadExecutor=Executors.newSingleThreadExecutor();
final ExecutorService imageDownloadExecutor=Executors.newFixedThreadPool(5);
...
//下载HTML和解压线程
typedownload=“html”;
最终下载任务htmlTask=DownloadUtil.downloadHtml(主视图控制器、dns、端口、uuid、文件路径HTMLDOWNLOAD、cookies、类型下载);
typedownload=“images”;
最终列表imageTasks=DownloadUtil.downloadImages(mainViewController、dns、端口、numImg、uuid、cookies、typeDownloaded);
//提交html下载转义
htmlDownloadExecutor.execute(新的Runnable(){
@凌驾
公开募捐{
//首先运行html任务
htmlTask.run();
//现在提交要运行的映像任务
for(下载任务imageTask:imageTasks){
imageDownloadExecutor.execute(imageTask);
}
}
});

助手函数已更改为返回任务实例,任务实例将在适当时提交给相应的executor服务实例。html下载任务会立即提交以下载html,一旦下载完成,下载图像的任务就会提交。此解决方案不涉及线程阻塞操作(CountDownLatch.await()),因此它应该更具可扩展性/性能

您可以延迟图像下载任务的提交,直到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);
            }

        }
    });
final ExecutorService htmlDownloadExecutor=Executors.newSingleThreadExecutor();
final ExecutorService imageDownloadExecutor=Executors.newFixedThreadPool(5);
...
//下载HTML和解压线程
typedownload=“html”;
最终下载任务htmlTask=DownloadUtil.downloadHtml(主视图控制器、dns、端口、uuid、文件路径HTMLDOWNLOAD、cookies、类型下载);
typedownload=“images”;
最终列表imageTasks=DownloadUtil.downloadImages(mainViewController、dns、端口、numImg、uuid、cookies、typeDownloaded);
//提交html下载转义
htmlDownloadExecutor.execute(新的Runnable(){
@凌驾
公开募捐{
//首先运行html任务
htmlTask.run();
//现在提交要运行的映像任务
for(下载任务imageTask:imageTasks){
imageDownloadExecutor.execute(imageTask);
}
}
});
助手函数已更改为返回任务实例,任务实例将在适当时提交给相应的executor服务实例。html下载任务会立即提交以下载html,一旦下载完成,下载图像的任务就会提交。没有线程阻塞操作(CountDo)