Running multiple tasks and processing all the results
The Executor
framework allows you to execute concurrent tasks without worrying about thread creation and execution. It provides you with the Future
class, which you can use to control the status and get the results of any task executed in an executor.
When you want to wait for the finalization of a task, you can use the following two methods:
- The
isDone()
method of theFuture
interface returnstrue
if the task has finished its execution - The
awaitTermination()
method of theThreadPoolExecutor
class puts the thread to sleep until all the tasks have finished their execution after a call to theshutdown()
method
These two methods have some drawbacks. With the first one, you can only control the completion of a task. With the second one, you have to shut down the executor to wait for a thread; otherwise, the method's call is returned immediately.
The ThreadPoolExecutor
class provides a method that allows you to send a list of tasks to the executor...