Types of thread pool
- FixedThreadPool: uses a blocking queue to store tasks
- CachedThreadPool: uses a synchronous queue of size 1
- ScheduledThreadPool: uses a delay queue
SingleThreadedExecuter (sequential execution of tasks): uses a blocking queue. The size of the thread pool is 1.
public class ThreadTypesExample { public static void main(String[] args) throws InterruptedException { ExecutorService service = Executors.newSingleThreadExecutor(); for (int i = 0; i < 3; i++) { service.execute(new Task()); } System.out.println("Thread name : " + Thread.currentThread().getName()); } static class Task implements Runnable { @Override public void run() { System.out.println("Thread name : " + Thread.currentThread().getName()); } } } //output Thread name : main Thread name : pool-1-thread-1 Thread name : pool-1-thread-1 Thread name : pool-1-thread-1