Java Executor Framework
Java provides its own multithreading framework called the Java Executor Framework which is introduced in JDK5 in java.util.concurrent package.
The Executor Framework in Java provides a high-level abstraction for managing concurrent tasks in a Java application. It simplifies multi-threading by handling tasks such as thread creation, management and task scheduling. One of its key features is the thread pool, which reuses existing threads instead of creating new ones, improving efficiency and resource management.?
Hierarchy of Executor :
The Key components of executor framework are :
executor.execute(threadObject);
Future<String> result = executorService.submit(callableTask);
ThreadPoolExecutor executor = new ThreadPoolExecutor(int mainPoolSize, long keepAliveTime,int maximumPoolSize, TimeUnit unit,BlockingQueue<Runnable> workQueue);
Types of Executor Framework:
1. SingleThreadExecutor:- It is designed to execute tasks sequentially, one at a time, on a single worker thread. This is useful in scenarios where you want to guarantee sequential execution of tasks, ensuring that they don't run concurrently. Single threaded pool is provided by the static newSingleThreadExecutor method of the Executors class.
ExecutorService executor = Executors.newSingleThreadExecutor();
executor.execute(new Runnable() {
public void run() {
// Task logic here
}
});
executor.shutdown();
领英推荐
2. FixedThreadPool(n): It creates a fixed-size thread pool with a specified number of worker threads.Fixed thread pool is provided by the static newFixedThreadPool method of the Executors class.
ExecutorService executor = Executors.newFixedThreadPool(5);
// Submit tasks for execution
for (int i = 0; i < 10; i++) {
executor.execute(new Runnable() {
public void run() {
// Task logic here
}
});
}
3. CachedThreadPool: It creates a thread pool that can dynamically adjust the number of worker threads based on the workload. It uses a SynchronousQueue queue. Some key points mentioned in below:?
ExecutorService executor = Executors.newCachedThreadPool();
// Submit tasks for execution
for (int i = 0; i < 10; i++) {
executor.execute(new Runnable() {
public void run() {
// Task logic here
}
});
}
4.ScheduledExecutor: Scheduled executors are based on the interface ScheduledExecutorService which extends the ExecutorService interface.It is specifically designed for scheduling tasks to run at specified times or with fixed time intervals.Some important key points explained in below:
Methods:?
schedule(Runnable ob, long delay, TimeUnit unit)
schedule(Callable<V> callable, long delay, TimeUnit unit)
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);
scheduledExecService.scheduleWithFixedDelay(Runnable command, long initialDelay, long period, TimeUnit unit) ;
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
// Schedule a task to run after a delay of 5 seconds
executor.schedule(new Runnable() {
public void run() {
// Task logic here
}
}, 5, TimeUnit.SECONDS);
Future Object: The result of the task submitted for execution to an executor can be accessed using the java.util.concurrent.The future object returned by the executor. Future can be thought of as a promise made to the caller by the executor. The future interface is mainly used to get the results of Callable results. whenever the task execution is completed, it is set in this Future object by the executor.
Future<String> result = executorService.submit(callableTask);