ThreadPoolExector 是什麼?

本文內容如有錯誤、不足之處,歡迎技術愛好者們一同探討,在本文下面討論區留言,感謝。

簡述

ThreadPoolExector 是 線程池執行器 ,用來構建線程池。

Java 線程池 (thread pool) 是管理工作線程的池化實現。它包含一個使任務等待執行的隊列。在 Java 中,可以使用ThreadPoolExecutor 來創建線程池。

Java 線程池管理可運行線程的集合。工作線程從隊列中執行可運行線程。java.util.concurrent.Executorsjava.util.concurrent.Executor 接口提供工廠和方法支持,方便在 Java 中創建線程池。

Executors 是一個實用程序工具類,它還提供了有用的方法,可以通過各種工廠方法與 ExecutorServiceScheduledExecutorServiceThreadFactoryCallable 等類共同使用。

原理

內部 ThreadPoolExecutor 包含的線程池可以包含不同數量的線程。池中的線程數由以下兩個變量確定:

  • corePoolSize
  • maximumPoolSize

如果 corePoolSize 將任務委派給線程池時在線程池中創建的線程少於線程,那麼即使該池中存在空閒線程,也會創建一個新線程。

如果內部任務隊列已滿,並且 corePoolSize 正在運行多個線程,但 maximumPoolSize 正在運行的線程少於線程,則將創建一個新線程來執行任務。

這是說明 ThreadPoolExecutor 原理的圖:
在這裏插入圖片描述
ThreadPoolExecutor 其中的一個構造方法(下面例子中將要使用到):

public ThreadPoolExecutor(int corePoolSize,
                  int maximumPoolSize,
                  long keepAliveTime,
                  TimeUnit unit,
                  BlockingQueue<Runnable> workQueue,
                  RejectedExecutionHandler handler)

參數

  • corePoolSize:線程池中的常駐核心線程數,除非allowCoreThreadTimeOut已設置
  • maximumPoolSize :線程池能夠容納同時執行的最大線程數,必須大於1
  • keepAliveTime :多餘的空閒線程的存活時間。當前線程池數量超過corePoolSize時,當空閒時間達到keepAliveTime值時,多餘空閒線程會被銷燬直到只剩下corePoolSize個線程爲止。
  • unit-:keepAliveTime參數的時間單位
  • workQueue:任務隊列,被提交但尚未被執行的任務。此隊列將僅保存Runnable 該 execute方法提交的任務。
  • handler :拒絕策略,因爲達到了線程界限和隊列容量而在執行被阻止時使用的處理程序。

拋出異常
如果下列條件之一成立將拋出 IllegalArgumentException 異常:

  • corePoolSize < 0
  • keepAliveTime < 0
  • maximumPoolSize <= 0
  • maximumPoolSize < corePoolSize

如果 workQueue 或 handler 爲 null 將拋出 NullPointerException 異常。

流程:

  1. 在創建線程池後,等待提交過來的任務請求。
  2. 當調用 execute() 方法添加一個請求任務時,線程池會做如下判斷:
    1. 如果正在運行的線程數量小於corePoolSize,那麼馬上分配線程運行這個任務;
    2. 如果正在運行的線程數量大於或等於 corePoolSize , 那麼將這個任務放入隊列;
    3. 如果這個時候等候隊列滿了,且正在運行的線程數量還小於 maximumPoolSIze ,那麼還是要創建非核心線程立刻運行這個任務;
    4. 如果隊列滿了,且正在運行的線程數量大於或等於 maximumPoolSIze ,那麼線程池會啓動飽和拒絕策略來執行。
  3. 當一個線程完成任務時,它會從隊列中取下一個任務來執行。
  4. 當一個線程無事可做超過一定時間(keepAliveTime)時,線程池會判斷:
    1. 如果當前運行的線程數大於 corePoolSize, 那麼這個線程就被停掉。
    2. 線程池的所有任務完成後,最終會收縮到 corePoolSize 的大小。

例子

RejectedExecutionHandler 接口的自定義實現

import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;

public class RejectedExecutionHandlerImpl implements RejectedExecutionHandler {

    @Override
    public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
        System.out.println(r.toString() + " is rejected");
    }

}

ThreadPoolExecutor 提供了幾種方法,通過這些方法可以找出執行程序的當前狀態,池大小,活動線程數和任務數。因此,有一個監視線程,該線程將在特定時間間隔打印執行程序信息。

import java.util.concurrent.ThreadPoolExecutor;

public class MyMonitorThread implements Runnable
{
    private ThreadPoolExecutor executor;
    private int seconds;
    private boolean run=true;

    public MyMonitorThread(ThreadPoolExecutor executor, int delay)
    {
        this.executor = executor;
        this.seconds=delay;
    }
    public void shutdown(){
        this.run=false;
    }
    @Override
    public void run()
    {
        while(run){
                System.out.println(
                    String.format("[monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, isShutdown: %s, isTerminated: %s",
                        this.executor.getPoolSize(),
                        this.executor.getCorePoolSize(),
                        this.executor.getActiveCount(),
                        this.executor.getCompletedTaskCount(),
                        this.executor.getTaskCount(),
                        this.executor.isShutdown(),
                        this.executor.isTerminated()));
                try {
                    Thread.sleep(seconds*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
        }
            
    }
}

使用 ThreadPoolExecutor 的線程池實現示例:

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class WorkerPool {

    public static void main(String args[]) throws InterruptedException{
        //RejectedExecutionHandler implementation
        RejectedExecutionHandlerImpl rejectionHandler = new RejectedExecutionHandlerImpl();
        //Get the ThreadFactory implementation to use
        ThreadFactory threadFactory = Executors.defaultThreadFactory();
        //creating the ThreadPoolExecutor
        ThreadPoolExecutor executorPool = new ThreadPoolExecutor(2, 4, 10, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(2), threadFactory, rejectionHandler);
        //start the monitoring thread
        MyMonitorThread monitor = new MyMonitorThread(executorPool, 3);
        Thread monitorThread = new Thread(monitor);
        monitorThread.start();
        //submit work to the thread pool
        for(int i=0; i<10; i++){
            executorPool.execute(new WorkerThread("cmd"+i));
        }
        
        Thread.sleep(30000);
        //shut down the pool
        executorPool.shutdown();
        //shut down the monitor thread
        Thread.sleep(5000);
        monitor.shutdown();
        
    }
}

初始化 ThreadPoolExecutor,將初始池線程數量保持爲2,最大池線程數量保持爲4,工作隊列線程數量保持爲2。因此,如果有4個正在運行的任務並且提交了更多任務,則工作隊列將僅容納更多任務中的2個,其餘的將由 RejectedExecutionHandlerImpl 處理。

程序輸出:

pool-1-thread-1 Start. Command = cmd0
pool-1-thread-4 Start. Command = cmd5
cmd6 is rejected
pool-1-thread-3 Start. Command = cmd4
pool-1-thread-2 Start. Command = cmd1
cmd7 is rejected
cmd8 is rejected
cmd9 is rejected
[monitor] [0/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 4, Completed: 0, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-4 End.
pool-1-thread-1 End.
pool-1-thread-2 End.
pool-1-thread-3 End.
pool-1-thread-1 Start. Command = cmd3
pool-1-thread-4 Start. Command = cmd2
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
[monitor] [4/2] Active: 2, Completed: 4, Task: 6, isShutdown: false, isTerminated: false
pool-1-thread-1 End.
pool-1-thread-4 End.
[monitor] [4/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [2/2] Active: 0, Completed: 6, Task: 6, isShutdown: false, isTerminated: false
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
[monitor] [0/2] Active: 0, Completed: 6, Task: 6, isShutdown: true, isTerminated: true
自定義線程池

儘管 Java 通過 Executor 框架具有非常強大的線程池功能。如果沒有 executor,則不應創建自己的自定義線程池。不鼓勵任何手動編寫線程池類。可以通過手動編寫線程池類來學習和創建它,下面給出 Java 中的此類線程池實現。

CustomThreadPool.java

import java.util.concurrent.LinkedBlockingQueue;
 
@SuppressWarnings("unused")
public class CustomThreadPool 
{
    //Thread pool size
    private final int poolSize;
     
    //Internally pool is an array
    private final WorkerThread[] workers;
     
    // FIFO ordering
    private final LinkedBlockingQueue<Runnable> queue;
 
    public CustomThreadPool(int poolSize) 
    {
        this.poolSize = poolSize;
        queue = new LinkedBlockingQueue<Runnable>();
        workers = new WorkerThread[poolSize];
 
        for (int i = 0; i < poolSize; i++) {
            workers[i] = new WorkerThread();
            workers[i].start();
        }
    }
 
    public void execute(Runnable task) {
        synchronized (queue) {
            queue.add(task);
            queue.notify();
        }
    }
 
    private class WorkerThread extends Thread {
        public void run() {
            Runnable task;
 
            while (true) {
                synchronized (queue) {
                    while (queue.isEmpty()) {
                        try {
                            queue.wait();
                        } catch (InterruptedException e) {
                            System.out.println("An error occurred while queue is waiting: " + e.getMessage());
                        }
                    }
                    task = (Runnable) queue.poll();
                }
 
                try {
                    task.run();
                } catch (RuntimeException e) {
                    System.out.println("Thread pool is interrupted due to an issue: " + e.getMessage());
                }
            }
        }
    }
 
    public void shutdown() {
        System.out.println("Shutting down thread pool");
        for (int i = 0; i < poolSize; i++) {
            workers[i] = null;
        }
    }
}

CustomThreadPoolExample.java

public class CustomThreadPoolExample 
{
    public static void main(String[] args) 
    {
        CustomThreadPool customThreadPool = new CustomThreadPool(2);
         
        for (int i = 1; i <= 5; i++) 
        {
            Task task = new Task("Task " + i);
            System.out.println("Created : " + task.getName());
 
            customThreadPool.execute(task);
        }
    }
}

輸出:

Created : Task 1
Created : Task 2
Created : Task 3
Created : Task 4
Created : Task 5
Executing : Task 1
Executing : Task 2
Executing : Task 3
Executing : Task 4
Executing : Task 5

以上是非常原始的線程池實現,具有很多改進的範圍。錯誤的池或隊列處理也可能導致死鎖或資源崩潰,正確使用經過Java社區測試良好的 Executor 框架,可以避免這些問題。

總結

ThreadPoolExector 類有4種不同的構造方法,考慮到它們的複雜性,Java 併發API中 Exectors 類來解決這個問題。建議使用 Exectors 來創建線程池,雖然可以手動通過 ThreadPoolExector 構造函數創建線程池。

參考資料

ThreadPoolExecutor

Java Thread Pool – ThreadPoolExecutor Example (Java線程池– ThreadPoolExecutor示例

ThreadPoolExecutor – Java Thread Pool Example(ThreadPoolExecutor – Java線程池示例

Class ThreadPoolExecutor

ThreadPoolExecutor Class(ThreadPoolExecutor類

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章