Wait for all tasks in ExecutorService.execute to complete
If you read the javadoc of the ExecutorService.awaitTermination
(or look at the method signature) you will see it returns a boolean
. This boolean indicates if the Executor
terminated or not. You can use that information to create a while
loop to determine if it has been terminated or not.
ExecutorService executor = ...
executor.shutdown(); // close the executor and don't accept new tasks
while (!executor.awaitTermination(100, TimeUnit.MILLISECONDS) {}
Something like this will stop the executor and wait until it terminated and all tasks have finished.
Await completion of all ExecutorService Threads
When I executed the code above, I was expecting the code to await the completion of the previous threads when running "threadPool.shutdown()"
this won't happen, from ExecutorService
's docs:
void shutdown()
Initiates an orderly shutdown in which previously submitted tasks are
executed, but no new tasks will be accepted. Invocation has no
additional effect if already shut down.This method does not wait for previously submitted tasks to complete
execution. UseawaitTermination
to do that.
in order to achieve your result, you need to use both, shutdown()
first and then awaitTermination()
:
boolean awaitTermination(long timeout, TimeUnit unit) throws
InterruptedExceptionBlocks until all tasks have completed execution after a shutdown
request, or the timeout occurs, or the current thread is interrupted,
whichever happens first.returns:
true if this executor terminated and false if the timeout elapsed before termination
this behaviour:
I tried using threadPool.awaitTermination(30, TimeUnit.SECONDS)
instead but for a reason I ignore yet, it awaits the full completion
of the 30 seconds before continuing even if all letters have been
printed.
was probably caused by replacing shutdown()
with awaitTermination()
. it was waiting on a "non-shutdown" pool. as already said, you need to call shutdown()
before awaitTermination()
.
ExecutorService's shutdown() doesn't wait until all threads will be finished
The answer is available in the ExecutorService.shutdown()
Javadoc:
This method does not wait for previously submitted tasks to complete execution. Use
awaitTermination
to do that.
If you want to wait for the threads to finish work you have the following options:
- get
Future
instances returned bysubmit()
and callget()
on everyFuture
instance - after calling
shutdown
onservice
callawaitTermination
onservice
until it returnstrue
- instead of calling
submit
onservice
add yourRunnable
instances to ajava.util.List
and pass this list to theinvokeAll
method called onservice
how to make main thread wait for executor service threads to complete
You can use CountDownLatch#await
. Eg copied from docs:
CountDownLatch startSignal = new CountDownLatch(1);
CountDownLatch doneSignal = new CountDownLatch(N);
for (int i = 0; i < N; ++i) // create and start threads
new Thread(new Worker(startSignal, doneSignal)).start();
doSomethingElse(); // don't let run yet
startSignal.countDown(); // let all threads proceed
doSomethingElse();
doneSignal.await(); // wait for all to finish
Wait main thread until all the thread pools task complete of ExecutorService?
The method you are looking for exactly is
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
throws InterruptedException
From the javadocs,
Executes the given tasks, returning a list of Futures holding their
status and results when all complete. Future.isDone() is true for each
element of the returned list. Note that a completed task could have
terminated either normally or by throwing an exception. The results of
this method are undefined if the given collection is modified while
this operation is in progress.
However, you will need to convert Runnable
to List<Callable<Void>>
to make use of this.
Also, I would use Timeouts for sanity when using the code in production, which has method signature of
<T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,
long timeout,
TimeUnit unit)
throws InterruptedException
for sanity!
Related Topics
Regex, Remove Whitespace and All Other Characters
Several Ports (8005, 8080, 8009) Required by Tomcat Server At Localhost Are Already in Use
How to Get Access to Job Parameters from Itemreader, in Spring Batch
How to Restart a Java Program Here
Regular Expression to Match a Backslash Followed by a Quote
How to Scroll to the Bottom of a Recyclerview? Scrolltoposition Doesn't Work
Delete Not Working in Hibernate/Spring Data
Round Off a Double While Maintaining the Trailing Zero
How to Return a List of Specific Type Instead of List<Object[]> in Hibernate
How to Do Post Request With Raw Data Via Spring Rest Template
How to Count the Rows in Hibernate Query Language
How to Send Text to Textarea in Div Element Using Selenium
Name a File in Java to Include Date and Time Stamp
Placing a Jlabel At a Specific X,Y Coordinate on a Jpanel
Delete a Line from a File Using Java
Failed to Process Import Candidates for Configuration Class
Method Does Not Override Method from Its Superclass . Android Fragment
I Get a Status 200 When Connecting to the Websocket, But It Is an Error