Reputation: 6556
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));
}
}
Upvotes: 1
Views: 106
Reputation: 1493
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.
Upvotes: 1
Reputation: 877
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()
Upvotes: 0