Question

I have one executor running in a singleThreadExecutor and I need that after it finish call other method (saveJsonFile()), but only when my executor finish

My class with executor

        ExecutorService e = Executors.newSingleThreadExecutor();

                    //Download HTML and Unzip Threads
                    typeDownloaded = "html";
                    DownloadUtil.downloadHtml(e, this, dns, port, offlineUuid, filePathHtmlDownload, cookies, typeDownloaded);
                    UnzipUtil.unZipHtml(e, this, filePathHtmlDownload, outputFolder, typeDownloaded);
                    e.shutdown();
                    saveJsonFile();

My UnzipUtil class:

public class UnzipUtil {

    public static void unZipHtml(Executor e, MainViewController controller, String zipFile, String outputFolder, String type) {
        e.execute(new UnzipTask(controller, zipFile, outputFolder, type));
    }

    public static void unZipImages(Executor e, MainViewController controller, int numImg, String outputFolder, String type) throws SystemException, PortalException {
        String filePath;

        filePath = System.getProperty("user.home") + File.separator + "Documents" + File.separator + "TargetApp" + File.separator + "TempImageDownload.zip";
        System.out.println(filePath);
        e.execute(new UnzipTask(controller, filePath, outputFolder, type));

    }
}
Was it helpful?

Solution 2

use submit instead of e.exectute. That will return a Future, result.get will block until your task is executed

Future<?> result = e.submit(...);
result.get() 

OTHER TIPS

Maybe I did not get the problem, but couldn't you use Futures and Callables? Basically, you can specify a "callback" that will be called when your unzip task completes... Quoting from the link (where you can also find a simple example on how to use the machinery):

A Future represents the result of an asynchronous computation. Methods are provided to check if the computation is complete, to wait for its completion, and to retrieve the result of the computation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top