Java thread pool (Executor Service) uses

I. Prerequisites

/**
 * The thread runs demo, and the runtime types out the thread id and the parameters in the incoming thread
 */
public class ThreadRunner implements Runnable {

    private final SimpleDateFormat format = new SimpleDateFormat("HH:mm:ss.SSS");

    /**
     * Thread private property, created when creating threads
     */
    private Integer num;

    public ThreadRunner(Integer num) {
        this.num = num;
    }

    @Override
    public void run() {
        System.out.println("thread:" + Thread.currentThread().getName() + ",time:" + format.format(new Date()) + ",num:" + num);
        try {//Sleep threads to simulate thread blocking
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

Classification

1. Fixed ThreadPool - has a fixed size thread pool

public class FixedThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService pool = Executors.newFixedThreadPool(4);
        for(int i = 0 ; i < 50 ; i++){
            pool.submit(new ThreadRunner((i + 1)));
        }
        pool.shutdown();
    }
}

Output:

thread:pool-1-thread-2,time:16:14:45.677,num:2
thread:pool-1-thread-4,time:16:14:45.678,num:4
thread:pool-1-thread-3,time:16:14:45.680,num:3
thread:pool-1-thread-1,time:16:14:45.684,num:1
thread:pool-1-thread-4,time:16:14:46.680,num:5
thread:pool-1-thread-2,time:16:14:46.680,num:6
thread:pool-1-thread-3,time:16:14:46.680,num:7
thread:pool-1-thread-1,time:16:14:46.684,num:8
thread:pool-1-thread-4,time:16:14:47.680,num:9
thread:pool-1-thread-2,time:16:14:47.680,num:10
thread:pool-1-thread-3,time:16:14:47.681,num:11
thread:pool-1-thread-1,time:16:14:47.684,num:12
thread:pool-1-thread-4,time:16:14:48.681,num:13
thread:pool-1-thread-2,time:16:14:48.681,num:14
thread:pool-1-thread-3,time:16:14:48.681,num:15
thread:pool-1-thread-1,time:16:14:48.684,num:16
thread:pool-1-thread-4,time:16:14:49.681,num:17
thread:pool-1-thread-2,time:16:14:49.682,num:18
thread:pool-1-thread-3,time:16:14:49.682,num:19
thread:pool-1-thread-1,time:16:14:49.684,num:20
thread:pool-1-thread-4,time:16:14:50.681,num:21
thread:pool-1-thread-2,time:16:14:50.682,num:22
thread:pool-1-thread-3,time:16:14:50.682,num:23
thread:pool-1-thread-1,time:16:14:50.684,num:24
thread:pool-1-thread-4,time:16:14:51.681,num:25
thread:pool-1-thread-2,time:16:14:51.682,num:26
thread:pool-1-thread-3,time:16:14:51.682,num:27
thread:pool-1-thread-1,time:16:14:51.684,num:28
thread:pool-1-thread-4,time:16:14:52.681,num:29
thread:pool-1-thread-2,time:16:14:52.682,num:30
thread:pool-1-thread-3,time:16:14:52.682,num:31
thread:pool-1-thread-1,time:16:14:52.684,num:32
thread:pool-1-thread-4,time:16:14:53.681,num:33
thread:pool-1-thread-2,time:16:14:53.682,num:34
thread:pool-1-thread-3,time:16:14:53.683,num:35
thread:pool-1-thread-1,time:16:14:53.685,num:36
thread:pool-1-thread-2,time:16:14:54.682,num:38
thread:pool-1-thread-4,time:16:14:54.682,num:37
thread:pool-1-thread-3,time:16:14:54.683,num:39
thread:pool-1-thread-1,time:16:14:54.686,num:40
thread:pool-1-thread-2,time:16:14:55.682,num:41
thread:pool-1-thread-4,time:16:14:55.682,num:42
thread:pool-1-thread-3,time:16:14:55.683,num:43
thread:pool-1-thread-1,time:16:14:55.686,num:44
thread:pool-1-thread-2,time:16:14:56.682,num:45
thread:pool-1-thread-4,time:16:14:56.683,num:46
thread:pool-1-thread-3,time:16:14:56.684,num:47
thread:pool-1-thread-1,time:16:14:56.686,num:48
thread:pool-1-thread-2,time:16:14:57.683,num:49
thread:pool-1-thread-4,time:16:14:57.683,num:50

Summary:
- The number of threads in the pool is fixed and does not change
- Using unbounded LinkedBlockingQueue, we need to consider both generation and consumption capacity, generating excess, which may lead to heap memory overflow.
- Applicable to some very stable and fixed regular concurrent threads, mostly for servers

2,CachedThreadPool

public class CachedThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService pool = Executors.newCachedThreadPool();
        for(int i = 0 ; i < 50 ; i++){
            pool.submit(new ThreadRunner((i + 1)));
        }
        pool.shutdown();
    }
}

Output:

thread:pool-1-thread-2,time:16:17:21.289,num:2
thread:pool-1-thread-3,time:16:17:21.290,num:3
thread:pool-1-thread-4,time:16:17:21.290,num:4
thread:pool-1-thread-6,time:16:17:21.291,num:6
thread:pool-1-thread-7,time:16:17:21.291,num:7
thread:pool-1-thread-8,time:16:17:21.291,num:8
thread:pool-1-thread-1,time:16:17:21.292,num:1
thread:pool-1-thread-10,time:16:17:21.293,num:10
thread:pool-1-thread-5,time:16:17:21.294,num:5
thread:pool-1-thread-11,time:16:17:21.294,num:11
thread:pool-1-thread-15,time:16:17:21.294,num:15
thread:pool-1-thread-9,time:16:17:21.294,num:9
thread:pool-1-thread-16,time:16:17:21.295,num:16
thread:pool-1-thread-20,time:16:17:21.295,num:20
thread:pool-1-thread-14,time:16:17:21.296,num:14
thread:pool-1-thread-12,time:16:17:21.296,num:12
thread:pool-1-thread-19,time:16:17:21.297,num:19
thread:pool-1-thread-13,time:16:17:21.299,num:13
thread:pool-1-thread-17,time:16:17:21.300,num:17
thread:pool-1-thread-18,time:16:17:21.302,num:18
thread:pool-1-thread-22,time:16:17:21.304,num:22
thread:pool-1-thread-23,time:16:17:21.304,num:23
thread:pool-1-thread-24,time:16:17:21.305,num:24
thread:pool-1-thread-21,time:16:17:21.305,num:21
thread:pool-1-thread-26,time:16:17:21.305,num:26
thread:pool-1-thread-25,time:16:17:21.306,num:25
thread:pool-1-thread-29,time:16:17:21.307,num:29
thread:pool-1-thread-28,time:16:17:21.308,num:28
thread:pool-1-thread-30,time:16:17:21.308,num:30
thread:pool-1-thread-34,time:16:17:21.308,num:34
thread:pool-1-thread-35,time:16:17:21.308,num:35
thread:pool-1-thread-33,time:16:17:21.308,num:33
thread:pool-1-thread-27,time:16:17:21.309,num:27
thread:pool-1-thread-32,time:16:17:21.308,num:32
thread:pool-1-thread-31,time:16:17:21.309,num:31
thread:pool-1-thread-36,time:16:17:21.310,num:36
thread:pool-1-thread-37,time:16:17:21.310,num:37
thread:pool-1-thread-38,time:16:17:21.310,num:38
thread:pool-1-thread-42,time:16:17:21.310,num:42
thread:pool-1-thread-40,time:16:17:21.310,num:40
thread:pool-1-thread-41,time:16:17:21.311,num:41
thread:pool-1-thread-47,time:16:17:21.762,num:47
thread:pool-1-thread-43,time:16:17:21.762,num:43
thread:pool-1-thread-39,time:16:17:21.762,num:39
thread:pool-1-thread-45,time:16:17:21.762,num:45
thread:pool-1-thread-44,time:16:17:21.763,num:44
thread:pool-1-thread-46,time:16:17:21.761,num:46
thread:pool-1-thread-48,time:16:17:21.761,num:48
thread:pool-1-thread-49,time:16:17:21.765,num:49
thread:pool-1-thread-50,time:16:17:21.765,num:50

summary
- Threads in the pool increase as processing data increases
- The number of threads does not always increase. If a new task needs to be executed, first query whether there are idle threads in the pool and until the idle deadline. If there are, use idle threads. If not, create new threads and put them into the pool.
- Used to perform asynchronous tasks with short lifetimes. It is not suitable for IO equal-length delay operation, because it may create a large number of threads, resulting in system crash.
- Using SynchronousQueue as a blocking queue, if a new task enters the queue, the data in the queue must be processed by other threads, otherwise it will wait.

3,SingleThreadExecutor

public class SingleThreadPoolDemo {

    public static void main(String[] args) {
        ExecutorService pool = Executors.newSingleThreadExecutor();
        for(int i = 0 ; i < 50 ; i++){
            pool.submit(new ThreadRunner((i + 1)));
        }
        pool.shutdown();
    }
}

Output:

thread:pool-1-thread-1,time:16:20:10.194,num:1
thread:pool-1-thread-1,time:16:20:11.197,num:2
thread:pool-1-thread-1,time:16:20:12.197,num:3
thread:pool-1-thread-1,time:16:20:13.197,num:4
thread:pool-1-thread-1,time:16:20:14.197,num:5
thread:pool-1-thread-1,time:16:20:15.198,num:6
thread:pool-1-thread-1,time:16:20:16.198,num:7
thread:pool-1-thread-1,time:16:20:17.198,num:8
thread:pool-1-thread-1,time:16:20:18.198,num:9
thread:pool-1-thread-1,time:16:20:19.198,num:10
thread:pool-1-thread-1,time:16:20:20.198,num:11
thread:pool-1-thread-1,time:16:20:21.199,num:12
thread:pool-1-thread-1,time:16:20:22.200,num:13
thread:pool-1-thread-1,time:16:20:23.200,num:14
thread:pool-1-thread-1,time:16:20:24.200,num:15
thread:pool-1-thread-1,time:16:20:25.200,num:16
thread:pool-1-thread-1,time:16:20:26.201,num:17
thread:pool-1-thread-1,time:16:20:27.201,num:18
thread:pool-1-thread-1,time:16:20:28.201,num:19
thread:pool-1-thread-1,time:16:20:29.201,num:20
thread:pool-1-thread-1,time:16:20:30.202,num:21
thread:pool-1-thread-1,time:16:20:31.202,num:22
thread:pool-1-thread-1,time:16:20:32.203,num:23
thread:pool-1-thread-1,time:16:20:33.203,num:24
thread:pool-1-thread-1,time:16:20:34.203,num:25
thread:pool-1-thread-1,time:16:20:35.203,num:26
thread:pool-1-thread-1,time:16:20:36.203,num:27
thread:pool-1-thread-1,time:16:20:37.203,num:28
thread:pool-1-thread-1,time:16:20:38.203,num:29
thread:pool-1-thread-1,time:16:20:39.203,num:30
thread:pool-1-thread-1,time:16:20:40.203,num:31
thread:pool-1-thread-1,time:16:20:41.203,num:32
thread:pool-1-thread-1,time:16:20:42.203,num:33
thread:pool-1-thread-1,time:16:20:43.204,num:34
thread:pool-1-thread-1,time:16:20:44.204,num:35
thread:pool-1-thread-1,time:16:20:45.204,num:36
thread:pool-1-thread-1,time:16:20:46.204,num:37
thread:pool-1-thread-1,time:16:20:47.205,num:38
thread:pool-1-thread-1,time:16:20:48.205,num:39
thread:pool-1-thread-1,time:16:20:49.205,num:40
thread:pool-1-thread-1,time:16:20:50.206,num:41
thread:pool-1-thread-1,time:16:20:51.206,num:42
thread:pool-1-thread-1,time:16:20:52.207,num:43
thread:pool-1-thread-1,time:16:20:53.207,num:44
thread:pool-1-thread-1,time:16:20:54.207,num:45
thread:pool-1-thread-1,time:16:20:55.207,num:46
thread:pool-1-thread-1,time:16:20:56.207,num:47
thread:pool-1-thread-1,time:16:20:57.208,num:48
thread:pool-1-thread-1,time:16:20:58.208,num:49
thread:pool-1-thread-1,time:16:20:59.209,num:50

Summary:
- Only one thread in a thread is executing
- For tasks with a clear execution sequence but without affecting the main thread, tasks pressed into the pool will be executed in queue order.
- Using unbounded LinkedBlockingQueue, we need to consider both generation and consumption capacity, generating excess, which may lead to heap memory overflow.

3. Source Code

public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
 public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

* ThreadPool Executor Construction Method*

public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue) {
    this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
         Executors.defaultThreadFactory(), defaultHandler);
}


 public ThreadPoolExecutor(int corePoolSize,
                          int maximumPoolSize,
                          long keepAliveTime,
                          TimeUnit unit,
                          BlockingQueue<Runnable> workQueue,
                          ThreadFactory threadFactory,
                          RejectedExecutionHandler handler) {
    if (corePoolSize < 0 ||
        maximumPoolSize <= 0 ||
        maximumPoolSize < corePoolSize ||
        keepAliveTime < 0)
        throw new IllegalArgumentException();
    if (workQueue == null || threadFactory == null || handler == null)
        throw new NullPointerException();
    this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;
}
  • corePoolSize: Number of Core Threads in Thread Pool
    1. If the number of threads in the pool is less than the number of core thread pools, new threads are created directly to handle the current task.
    2. Core thread pools are not reclaimed when they are idle.
    3. When there are no idle threads in the pool, new tasks are added to the blocking queue
  • Maximum PoolSize: Maximum number of threads in the thread pool
    1. When the blocking queue is full and a new task is still on the queue, create a new thread processing until the number of threads is greater than maximumPoolSize.
    2. Threads that exceed the corePoolSize section are reclaimed after they have exceeded their idle time
    3. When the thread has exceeded corePoolSize and the queue capacity is full, it refuses to join the queue.
  • keepAliveTime unit: Thread lifetime
    1. Effective when the thread exceeds corePoolSize
    2. When the thread is free to keep AliveTime, it will be reclaimed
  • workQueue: Threads use blocking queues
  • threadFactory: Create Thread Pool Factory
    1. Additional logic to control the creation or destruction of threads
  • handler: Thread pool rejection policy
    1. Discard Policy
    2. Discard Oldest Policy, the oldest task in the queue, is discarded.
    3. Abort Policy
    4. Assign tasks to calling threads for execution (CallerRunsPolicy)

Keywords: less

Added by rcoursey on Sun, 09 Jun 2019 01:28:25 +0300