Learn to cancel a task submitted to an executor service if the task still has to be executed and/or has not been completed yet. We can use the cancel() method of Future object that allows making the cancellation requests. 1. Future cancel() API The Future.cancel() method takes one argument of type boolean. boolean cancel(boolean mayInterruptIfRunning); Depending on the value of mayInterruptIfRunning and the status of the task submitted…
Learn to run multiple Callable tasks with ExecutorService.invokeAll() API and processing all the results returned from tasks in form of Future class instances in this ExecutorService Callable example. 1. ExecutorService invokeAll() API The invokeAll() method executes the given list of Callable tasks, returning a list of Future objects holding their status and results when all are complete. It is an overloaded method and is in two forms. The second method takes extra…
In simple words, concurrency is the ability to run several programs or several parts of a program in parallel. Concurrency enables a program to achieve high performance and throughput by utilizing the untapped capabilities of the underlying operating system and machine hardware. For example, modern computers have several CPUs or several cores within one CPU, the program…