- Methods that create and return an ExecutorService set up with commonly useful configuration settings.
- Methods that create and return a ScheduledExecutorService set up with commonly useful configuration settings.
- Methods that create and return a "wrapped" ExecutorService, that disables reconfiguration by making implementation-specific methods inaccessible.
- Methods that create and return a ThreadFactory that sets newly created threads to a known state.
- Methods that create and return a Callable out of other closure-like forms, so they can be used in execution methods requiring Callable.
- Executors.newFixedThreadPool(int nThreads) Method
- Executors.newSingleThreadExecutor() Method
- Executors.newCachedThreadPool() Method
- Executors.newScheduledThreadPool Method
- Executors.newSingleThreadScheduledExecutor() Method
1. Executors.newFixedThreadPool(int nThreads) Method
ExecutorService executorService = Executors.newFixedThreadPool(noOfThreads);
Java newFixedThreadPool example
class Task implements Runnable {
@Override
public void run() {
for (int i = 0; i < 5; i++) {
System.out.println("[" + Thread.currentThread().getName() + "] " + "Message " + i);
try {
Thread.sleep(200);
} catch (final InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class FixedThreadPoolExample {
public static void main(final String[] args) throws InterruptedException, ExecutionException {
System.out.println("Thread main started");
final ExecutorService executorService = Executors.newFixedThreadPool(2);
executorService.execute(new Task());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.execute(new Task());
executorService.shutdown();
System.out.println("Thread main finished");
}
}
Thread main started
[pool-1-thread-1] Message 0
[pool-1-thread-2] Message 0
Thread main finished
[pool-1-thread-1] Message 1
[pool-1-thread-2] Message 1
[pool-1-thread-1] Message 2
[pool-1-thread-2] Message 2
[pool-1-thread-1] Message 3
[pool-1-thread-2] Message 3
[pool-1-thread-2] Message 4
[pool-1-thread-1] Message 4
[pool-1-thread-1] Message 0
[pool-1-thread-2] Message 0
[pool-1-thread-1] Message 1
[pool-1-thread-2] Message 1
[pool-1-thread-1] Message 2
[pool-1-thread-2] Message 2
[pool-1-thread-1] Message 3
[pool-1-thread-2] Message 3
[pool-1-thread-1] Message 4
[pool-1-thread-2] Message 4
[pool-1-thread-1] Message 0
[pool-1-thread-1] Message 1
[pool-1-thread-1] Message 2
[pool-1-thread-1] Message 3
[pool-1-thread-1] Message 4
2. Executors.newSingleThreadExecutor() Method
ExecutorService executorService = Executors.newSingleThreadExecutor();
Executors.newSingleThreadExecutor() Method Example
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
/*
* Instantiates a thread pool with a single thread
* All tasks are executed sequentially by the same thread
*/
public class SingleThreadPoolExample {
public static void main(String[] args) throws InterruptedException, ExecutionException {
System.out.println("Thread main started");
Runnable task1 = () -> {
System.out.println("Executing Task1 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
Runnable task2 = () -> {
System.out.println("Executing Task2 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
Runnable task3 = () -> {
System.out.println("Executing Task3 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
final ExecutorService executorService = Executors.newSingleThreadExecutor();
System.out.println("Submitting the tasks for execution...");
executorService.submit(task1);
executorService.submit(task2);
executorService.submit(task3);
executorService.shutdown();
System.out.println("Thread main finished");
}
}
Thread main started
Submitting the tasks for execution...
Executing Task1 inside : pool-1-thread-1
Thread main finished
Executing Task2 inside : pool-1-thread-1
Executing Task3 inside : pool-1-thread-1
3. Executors.newCachedThreadPool() Method
final ExecutorService executorService = Executors.newCachedThreadPool();
Executors.newCachedThreadPool() Method Example
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class CachedThreadPoolExample {
public static void main(final String[] args) throws InterruptedException, ExecutionException {
System.out.println("Thread main started");
Runnable task1 = () -> {
System.out.println("Executing Task1 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
Runnable task2 = () -> {
System.out.println("Executing Task2 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(4);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
Runnable task3 = () -> {
System.out.println("Executing Task3 inside : " + Thread.currentThread().getName());
try {
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException ex) {
throw new IllegalStateException(ex);
}
};
final ExecutorService executorService = Executors.newCachedThreadPool();
System.out.println("Submitting the tasks for execution...");
executorService.submit(task1);
executorService.submit(task2);
executorService.submit(task3);
executorService.shutdown();
System.out.println("Thread main finished");
}
}
Thread main started
Submitting the tasks for execution...
Executing Task1 inside : pool-1-thread-1
Executing Task3 inside : pool-1-thread-3
Executing Task2 inside : pool-1-thread-2
Thread main finished
4. Executors.newScheduledThreadPool Method
public class SchedulingTasksWithScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
System.out.println("Thread main started");
// Create a task
Runnable task1 = () -> {
System.out.println("Executing the task1 at: " + new Date());
};
// Create a task
Runnable task2 = () -> {
System.out.println("Executing the task2 at: " + new Date());
};
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
System.out.println("Scheduling task to run after 5 seconds... " + new Date());
scheduledExecutorService.schedule(task1, 5, TimeUnit.SECONDS);
scheduledExecutorService.schedule(task2, 5, TimeUnit.SECONDS);
scheduledExecutorService.shutdown();
System.out.println("Thread main finished");
}
}
Thread main started
Scheduling task to run after 5 seconds... Sat Sep 01 10:56:40 IST 2018
Thread main finished
Executing the task1 at: Sat Sep 01 10:56:45 IST 2018
Executing the task2 at: Sat Sep 01 10:56:45 IST 2018
public class SchedulingTasksWithScheduledThreadPool {
public static void main(String[] args) throws InterruptedException {
System.out.println("Thread main started");
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
// Create a task
Runnable task1 = () -> {
System.out.println("Executing the task1 at: " + new Date());
};
scheduledExecutorService.scheduleAtFixedRate(task1, 0, 2, TimeUnit.SECONDS);
System.out.println("Thread main finished");
}
}
Thread main started
Thread main finished
Executing the task1 at: Sat Sep 01 11:03:16 IST 2018
Executing the task1 at: Sat Sep 01 11:03:18 IST 2018
Executing the task1 at: Sat Sep 01 11:03:20 IST 2018
Executing the task1 at: Sat Sep 01 11:03:22 IST 2018
Executing the task1 at: Sat Sep 01 11:03:24 IST 2018
......
5. Executors.newSingleThreadScheduledExecutor() Method
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
Executors.newSingleThreadScheduledExecutor() Method Example
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ExecutorsDemo {
public static void main(String[] args) {
ExecutorsDemo demo = new ExecutorsDemo();
demo.newSingleThreadScheduledExecutor();
}
private void newSingleThreadScheduledExecutor() {
System.out.println("Thread main started");
ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
// Create a task
Runnable task1 = () -> {
System.out.println("Executing the task1 at: " + new Date());
};
scheduledExecutorService.scheduleAtFixedRate(task1, 0, 2, TimeUnit.SECONDS);
System.out.println("Thread main finished");
}
}
Thread main started
Thread main finished
Executing the task1 at: Mon Sep 10 13:15:57 IST 2018
Executing the task1 at: Mon Sep 10 13:15:59 IST 2018
Executing the task1 at: Mon Sep 10 13:16:01 IST 2018
Executing the task1 at: Mon Sep 10 13:16:03 IST 2018
Executing the task1 at: Mon Sep 10 13:16:05 IST 2018
Executing the task1 at: Mon Sep 10 13:16:07 IST 2018
...............
Learn multi-threading on Java Multithreading Tutorial
Learn advance concurrency on Java Concurrency Tutorial
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner

Comments
Post a Comment
Leave Comment