ExecutorService provides a powerful framework to manage and control threads more efficiently than manual thread creation.
Source Code
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class ThreadPoolExample {
public static void main(String[] args) {
ExecutorService executor = Executors.newFixedThreadPool(2);
Runnable task1 = () -> System.out.println("Executing Task1");
Runnable task2 = () -> System.out.println("Executing Task2");
executor.execute(task1);
executor.execute(task2);
executor.shutdown();
}
}