Executor Framework
Last updated
Last updated
Introduced in Java 5.
Why Executor Framework?
If you have thousands of task to be executed and if you create each thread for thousands of tasks, you will get performance overheads as creation and maintenance of each thread is an overhead.
Executor framework solves this problem.
In executor framework, you can create specified number of threads and reuse them to execute more tasks once it completes its current task.
It simplifies the design of creating multithreaded application and manages thread life cycles.
The programmer does not have to create or manage threads themselves, that’s the biggest advantage of executor framework.
Important classes/interfaces for executor framework.
java.util.concurrent.Executor
This interface is used to submit new task. It has a method called execute()
.
java.util.concurrent.ExecutorService
It is sub-interface of Executor.
Provides methods for submitting/executing Callable
/Runnable
tasks, shutting down service, executing multiple tasks etc.
Read about callable task
java.util.concurrent.ScheduledExecutorService
It is sub-interface of executor service which provides methods for scheduling tasks at fixed intervals/delay along with initial delay.
java.util.concurrent.Executors
This class provides factory methods for creating thread pool based executors.
Important factory methods (static method returning instance of ExecutorService) of Executors are:
newFixedThreadPool
This method returns thread pool executor whose maximum size is fixed. If all n
threads are busy performing the task and additional tasks are submitted, then they will have to wait in the task queue until thread is available.
The core pool size determines how many threads (idle) will be created when the web container starts. Max pool size is the maximum number of threads that can be created by the web container.
server.xml
consists of the executor framework related configuration in tomcat server.
Once a servicing is done for a client request, the pooled out thread is returned back to pool, rather than killing the thread. This approach helps in reusable thread management.
Note, once the core pool size is reached a new thread is added to thread pool.
If all the threads are busy servicing the clients, then new client request will be added to task queue and will be waiting for free thread. Once a free thread becomes available, then the queued task will be allocated the free thread and request will be further processed.
If the task queue also becomes full, then new connections are rejected untill current requests are processed and thread becomes available or task queue gets emptied.
Once the server is shutdown all these are destroyed and resources are cleaned up.
newCachedThreadPool
This method returns an unbounded thread pool. It doesn’t have maximum size but if it has less number of tasks, then it will tear down unused thread. If a thread has been unused for keepAliveTime (60 seconds), then it will tear it down.
newSingleThreadedExecutor
This method returns an executor which is guaranteed to use the single thread.
newScheduledThreadPool
This method returns a fixed size thread pool that can schedule commands to run after a given delay, or to execute periodically.
Steps for Runnable
Create a thread-pool executor, using suitable factory method of Executors.
eg : For fixed no of threads ExecutorService executor = Executors.newFixedThreadPool(10);
Create Runnable task
Use inherited method public void execute(Runnable command) Executes this Runnable task , in a separate thread.
Shutdown the service public void shutdown()
Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs.
List shutdownNow()
Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
BUT disadvantages with Runnable interface
Can't return result from the running task
Doesn't include throws Exception .
Better API java.util.concurrent.Callable V : result type of call method Represents a task that returns a result and may throw an exception. Functional i/f SAM : public V call() throws Exception Computes a result, or throws an exception if unable to do so.
Steps in using Callable i/f
Create a thread-pool executor , using suitable factory method of Executors.
eg : For fixed no of threads ExecutorService executor = Executors.newFixedThreadPool(10);
Create Callable task , which returns a result.
To submit a task to executor service , use method of ExecutorService i/f : public Future submit(Callable task) Submits a value-returning task for execution and returns a Future representing the pending results of the task. It's a non blocking method (i.e rets immediately)
The Future's get method will return the task's result upon successful completion.
If you would like to immediately block waiting for a task, invoke get() on Future. eg : result = exec.submit(aCallable).get();
OR main thread can perform some other jobs in the mean time & then invoke get on Future , to actually get the results. (get : blocking call ,waits till the computation is completed n then rets result)
Other methods of ExecutorService i/f
public List<Future> invokeAll(Collection<? extends Callable> tasks) throws InterruptedException
It's a blocking call.(waits till all tasks are complete) Executes the given tasks, returning a list of Futures holding their status and results when all complete. Future.isDone() is true for each element of the returned list.
Shutdown the service public void shutdown() Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
boolean awaitTermination(long timeout,TimeUnit unit) throws InterruptedException Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs.
List shutdownNow() Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.