Java Executor framework tutorial with example
Java 5 has introduced new framework called “Executor Framework” for managing threads.We have already seen before how to create a thread.
If you have noted, we need to create an object of thread class using new Thread(runnableObject), so we need to create thread object for each task.Imagine a situation where you have thousands of task to be executed and if you create each thread for thousands of tasks, you may get performance overheads as creation and maintenance of each thread is also an overhead. Executor framework will solve the problem. In executor framework, you can create specified number of threads and reuse them to execute more tasks once it completes its current task.
Executor framework 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.
There are some important classes or interfaces for executor framework.
If you have noted, we need to create an object of thread class using new Thread(runnableObject), so we need to create thread object for each task.Imagine a situation where you have thousands of task to be executed and if you create each thread for thousands of tasks, you may get performance overheads as creation and maintenance of each thread is also an overhead. Executor framework will solve the problem. In executor framework, you can create specified number of threads and reuse them to execute more tasks once it completes its current task.
Executor framework 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.
There are some important classes or interfaces for executor framework.
Executor
This interface is used to submit new task.It has a method called “execute”.
ExecutorService
It is sub-interface of Executor.This interface provides methods to manage lifecycle of tasks as well for executor.
For example It provides method for shutting down executors.
ScheduledExecutorService
It is sub-interface of executor service which provides methods for scheduling tasks at fixed intervals or with initial delay.
Executors
This class provides factory methods for creating thread pool.
Important factory methods of Executors are:
Important factory methods of Executors are:
newFixedThreadPool: This method returns thread pool executor whose maximum size(let’s say n threads) is fixed.If all n threads are busy performing the task and additional tasks are submitted, then they will have to be in the queue until thread is available.
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 1 mins(keepAliveTime), 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.
ThreadPoolExecutor
ThreadPoolExecutor is actual implementation of ThreadPool. It extends AbstractThreadPoolExecutor which implements ExecutorService interface. You can create ThreadPoolExecutor from factory methods of Executor class. It is recommended a way to get an instance of ThreadPoolExecutor as seen above.