In Java, Executor class does not have shutdown/shutdownNow()/awaitTermination like ExecutorService subclass. So if you start a task/thread by invoking executorObject.execute(runnableTask), how can you check if that task completed ?
You can't do it with Executor
simply because it provides a single method void execute(Runnable)
. Unless you consider using Executor
implementations that return Future
, you can implement notify/wait mechanism of your own:
final CountDownLatch latch = new CountDownLatch(1);
Runnable task = () -> {
try {
// ... do useful work
} finally {
latch.countDown();
}
}
executorObject.execute(task);
// wrap into try/catch for InterruptedException
// if not propagating further
latch.await(); // await(timeout);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.