I had some queries regarding Future
usage. Please go through below example before addressing my queries.
http://javarevisited.blogspot.in/2015/01/how-to-use-future-and-futuretask-in-Java.html
- The main purpose of using thread pools &
Executors
is to execute task asynchronously without blocking main thread. But once you useFuture
, it is blocking calling thread. Do we have to create separate new thread/thread pool to analyse the results ofCallable
tasks? OR is there any other good solution? - Since
Future
call is blocking the caller, is it worth to use this feature? If I want to analyse the result of a task, I can have synchronous call and check the result of the call withoutFuture
. - What is the best way to handle Rejected tasks with usage of
RejectionHandler
? If a task is rejected, is it good practice to submit the task to another Thread or ThreadPool Or submit the same task to currentThreadPoolExecutor
again?
Please correct me if my thought process is wrong about this feature.
Your question is about performing an action when an asynchronous action has been done.
Future
s on the other hand are good if you have an unrelated activity which you can perform while the asynchronous action is running. Then you may regularly poll the action represented by theFuture
viaisDone()
and do something else if not or call the blockingget()
if you have no more unrelated work for your current thread.If you want to schedule an on-completion action without blocking the current thread, you may instead use
CompletableFuture
which offers such functionality.