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类

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值