ExecutorServiceAndThreadPools

This Article is part of Series of Articles on Java 8 Concurrency Tutorial.

In this quick article, we’ll focus on a the concept of ExecutorService Framework in the Java language.

ExecutorService is a framework simplifies the task of creating threads and managing thread life cycle. ExecutorService is an interface, We need its implementations in order to make any use of it.

ThreadPoolExecutor & ScheduledThreadPoolExecutor implementations are available in java concurrent package.

Creating an ExecutorService:

Executors factory methods are available for creating ExecutorService.

ExecutorService executorService1 = Executors.newSingleThreadExecutor();
ExecutorService executorService2 = Executors.newFixedThreadPool(10);
ExecutorService executorService3 = Executors.newCachedThreadPool();

newCachedThreadPool

newCachedThreadPool method creates an executor having an expandable thread pool.Whenever a thread is needed, pool returns a thread from cache and if not available, a new thread is created for a short time. When the timeout of thread is over, that thread is vanished.

In below example 10 Threds will run Simultaneously

newCachedThreadPool Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 class MyWorker implements Runnable
{
private int id;
public MyWorker(int id) {
this.id=id;
}
@Override
public void run() {
System.out.println("MyWorker id " + id + " IS Working" + "Start Time " + System.currentTimeMillis());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ExecutorServiceExample {
public static void main(String[] args) {
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
IntStream.range(0, 10)
.forEach(cnt->newCachedThreadPool.execute(new MyWorker(cnt)));
newCachedThreadPool.shutdown();
}
}

newFixedThreadPool

newFixedThreadPool method Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue..

In below example 5 Threds will run Simultaneously. After Complition of task same 5 threds will be used for next 5 taks

newFixedThreadPool Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 class MyWorker implements Runnable
{
private int id;
public MyWorker(int id) {
this.id=id;
}
@Override
public void run() {
System.out.println("MyWorker id " + id + " IS Working" + "Start Time " + System.currentTimeMillis());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ExecutorServiceExample {
public static void main(String[] args) {
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(5);
IntStream.range(0, 10)
.forEach(cnt->newFixedThreadPool.execute(new MyWorker(cnt)));
newFixedThreadPool.shutdown();
}
}

newSingleThreadExecutor

newSingleThreadExecutor method Creates an Executor that uses a single worker thread operating off an unbounded queue.

In below example 1 Thread will run Simultaneously. After Complition of task same threds will be used for next 10 taks

newSingleThreadExecutor Example
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 class MyWorker implements Runnable
{
private int id;
public MyWorker(int id) {
this.id=id;
}
@Override
public void run() {
System.out.println("MyWorker id " + id + " IS Working" + "Start Time " + System.currentTimeMillis());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class ExecutorServiceExample {
public static void main(String[] args) {
ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
IntStream.range(0, 10)
.forEach(cnt->newSingleThreadExecutor.execute(new MyWorker(cnt)));
newSingleThreadExecutor.shutdown();
}
}

Key Points

shutdown()
An ExecutorService can be shut down, which will cause it to reject new tasks. Two different methods are provided for shutting down an ExecutorService.
The shutdown() method will allow previously submitted tasks to execute before terminating, while the shutdownNow() method prevents waiting tasks from starting and attempts to stop currently executing tasks.

execute()
Executes the given command at some time in the future. The command may execute in a new thread, in a pooled thread, or in the calling thread, at the discretion of the Executor implementation.
execute method only takes runnable task.

Share Comments