Im using @Async annotation for method that execute rsync command. There are ten threads calling this method at a time. My requirement is after all ten threads complete rsync command execution then only my remaining code should execute but not getting how to check whether my all ten threads has executed @Async method completely or not? So please tell me a way to check it
Asked
Active
Viewed 4.2k times
18
-
1Write an entry in a database or pass a message to a queue. – beerbajay Mar 21 '15 at 10:53
1 Answers
49
If you are going to return some value, you should wrap your return value into Standard Java SE Future
or Spring's AsyncResult
, which implements Future
also.
Something like this:
@Component
class AsyncTask {
@Async
public Future<String> call() throws InterruptedException {
return new AsyncResult<String>("return value");
}
}
If you do have this in place, in caller you do something like:
public void kickOffAsyncTask() throws InterruptedException {
Future<String> futureResult = asyncTask.call();
//do some stuff in parallel
String result = futureResult.get();
System.out.println(result);
}
Call futureResult.get()
will block caller thread and wait until your async thread finishes.
Optionally you can use Future.get(long timeout, TimeUnit unit)
if you don't want to wait forever.
EDIT:
If you don't need to return any value, I would still suggest to consider returning dummy return value. You don't need to use it for anything, just use to indicate that particular thread completed. Something like this:
public void kickOffAsyncTasks(int execCount) throws InterruptedException {
Collection<Future<String>> results = new ArrayList<>(execCount);
//kick off all threads
for (int idx = 0; idx < execCount; idx++) {
results.add(asyncTask.call());
}
// wait for all threads
results.forEach(result -> {
try {
result.get();
} catch (InterruptedException | ExecutionException e) {
//handle thread error
}
});
//all threads finished
}

luboskrnac
- 23,973
- 10
- 81
- 92
-
Im not returning any value what i was doing is running a for loop for 150 folders (1 folder per loop iteration) for sync the data using rsync command with @Async and my requirement is after rsync completed for all 150 folders need to parse data in the folder – DnA Mar 21 '15 at 15:19
-
2I'd just like to mention that if you used `CompletableFuture`, your `// wait for all threads` block could be replaced by `CompletableFuture.allOf(results).join()`. – Adam Hosman Sep 10 '21 at 22:40