Issue
I am using an external library that has the code from below. I am sending a lot of commands and am interesed in the result for statistics to check how many calls failed and how many succeeded
public Future<CommandResult> sendCommand(Command command) {
return command.execute();
}
CommandResult can be success or failure
However, if I use client.sendCommand(command).get();
then, I am waiting for the result synchronously, meanwhile the app is being blocked.
I would like to check only later (after 30 seconds which calls succeded and which failed). I am guaranteed to get an answer in 10 seconds. The problem is that the app waits for the computation to complete, and then retrieves its result.
I was thinking about this approach based on the answers:
List<Future< CommandResult >> futures = new ArrayList<>();
for(Command command: commands) {
futures.add(client.sendCommand(command));
}
//in a scheduler, 30+ seconds later
for (Future<Boolean> future : futures) {
saveResult(future.get());
}
Solution
I would like to check only later (after 30 seconds which calls succeeded and which failed). I am guaranteed to get an answer in 10 seconds. The problem is that the app waits for the computation to complete, and then retrieves its result.
If you want check on the results at a later time then your solution with Future<Boolean>
should be fine. The jobs will run in the background and you will get the results form then when you call future.get()
. Each of those get()
calls do block however.
If you want to get the results as they come in, I would use an ExecutorCompletionService
which you can poll anytime to see if you have results. The poll is non-blocking.
// create your thread pool using fixed or other pool
Executor<Result> threadPool = Executors.newFixedThreadPool(5);
// wrap the Executor in a CompletionService
CompletionService<Boolean> completionService =
new ExecutorCompletionService<>(e);
// submit jobs to the pool through the ExecutorCompletionService
for (Job job : jobs) {
completionService.submit(job);
}
// after we have submitted all of the jobs we can shutdown the Executor
// the jobs submitted will continue to run
threadPool.shutdown();
...
// some point later you can do
int jobsRunning = jobs.size();
for (int jobsRunning = jobs.size(); jobsRunning > 0; ) {
// do some processing ...
// are any results available?
Boolean result = completionService.poll();
if (result != null) {
// process a result if available
jobsRunning--;
}
}
Note that you will need to track how many jobs you submitted to the CompletionService
.
Answered By - Gray
Answer Checked By - Willingham (JavaFixing Volunteer)