threadpoolexecutor是jdk中的线程池实现,这个类实现了一个线程池需要的各个方法,它提供了任务提交、线程管理、监控等方法。

下面是threadpoolexecutor类的构造方法源码,其他创建线程池的方法最终都会导向这个构造方法,共有7个参数:corepoolsize、maximumpoolsize、keepalivetime、unit、workqueue、threadfactory、handler。

 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.acc = system.getsecuritymanager() == null ?
                null :
                accesscontroller.getcontext();
        this.corepoolsize = corepoolsize;
        this.maximumpoolsize = maximumpoolsize;
        this.workqueue = workqueue;
        this.keepalivetime = unit.tonanos(keepalivetime);
        this.threadfactory = threadfactory;
        this.handler = handler;
    }

这些参数都通过volatile修饰:

public class threadpoolexecutor extends abstractexecutorservice {
    private final blockingqueue<runnable> workqueue;
    private volatile threadfactory threadfactory;
    private volatile rejectedexecutionhandler handler;
    private volatile long keepalivetime;
    // 是否允许核心线程被回收
    private volatile boolean allowcorethreadtimeout;
    private volatile int corepoolsize;
    private volatile int maximumpoolsize;
}

corepoolsize:核心线程数

线程池维护的最小线程数量,核心线程创建后不会被回收(注意:设置allowcorethreadtimeout=true后,空闲的核心线程超过存活时间也会被回收)。

大于核心线程数的线程,在空闲时间超过keepalivetime后会被回收。

线程池刚创建时,里面没有一个线程,当调用 execute() 方法添加一个任务时,如果正在运行的线程数量小于corepoolsize,则马上创建新线程并运行这个任务。

maximumpoolsize:最大线程数

线程池允许创建的最大线程数量。

当添加一个任务时,核心线程数已满,线程池还没达到最大线程数,并且没有空闲线程,工作队列已满的情况下,创建一个新线程,然后从工作队列的头部取出一个任务交由新线程来处理,而将刚提交的任务放入工作队列尾部。

keepalivetime:空闲线程存活时间

当一个可被回收的线程的空闲时间大于keepalivetime,就会被回收。

可被回收的线程:

设置allowcorethreadtimeout=true的核心线程。大于核心线程数的线程(非核心线程)。

unit:时间单位

keepalivetime的时间单位:

timeunit.nanoseconds
timeunit.microseconds
timeunit.milliseconds // 毫秒
timeunit.seconds
timeunit.minutes
timeunit.hours
timeunit.days

workqueue:工作队列

新任务被提交后,会先添加到工作队列,任务调度时再从队列中取出任务。工作队列实现了blockingqueue接口。

jdk默认的工作队列有五种:

1.arrayblockingqueue 数组型阻塞队列:数组结构,初始化时传入大小,有界,fifo,使用一个重入锁,默认使用非公平锁,入队和出队共用一个锁,互斥。

2。linkedblockingqueue 链表型阻塞队列:链表结构,默认初始化大小为integer.max_value,有界(近似无解),fifo,使用两个重入锁分别控制元素的入队和出队,用condition进行线程间的唤醒和等待。

3.synchronousqueue 同步队列:容量为0,添加任务必须等待取出任务,这个队列相当于通道,不存储元素。

4.priorityblockingqueue 优先阻塞队列:无界,默认采用元素自然顺序升序排列。

5.delayqueue 延时队列:无界,元素有过期时间,过期的元素才能被取出。

threadfactory:线程工厂

创建线程的工厂,可以设定线程名、线程编号等。

默认线程工厂:

  /**
     * the default thread factory
     */
    static class defaultthreadfactory implements threadfactory {
        private static final atomicinteger poolnumber = new atomicinteger(1);
        private final threadgroup group;
        private final atomicinteger threadnumber = new atomicinteger(1);
        private final string nameprefix;
 
        defaultthreadfactory() {
            securitymanager s = system.getsecuritymanager();
            group = (s != null) ? s.getthreadgroup() :
                                  thread.currentthread().getthreadgroup();
            nameprefix = "pool-" +
                          poolnumber.getandincrement() +
                         "-thread-";
        }
 
        public thread newthread(runnable r) {
            thread t = new thread(group, r,
                                  nameprefix + threadnumber.getandincrement(),
                                  0);
            if (t.isdaemon())
                t.setdaemon(false);
            if (t.getpriority() != thread.norm_priority)
                t.setpriority(thread.norm_priority);
            return t;
        }
    }

handler:拒绝策略

当线程池线程数已满,并且工作队列达到限制,新提交的任务使用拒绝策略处理。可以自定义拒绝策略,拒绝策略需要实现rejectedexecutionhandler接口。

jdk默认的拒绝策略有四种:

1.abortpolicy:丢弃任务并抛出rejectedexecutionexception异常。

2.discardpolicy:丢弃任务,但是不抛出异常。可能导致无法发现系统的异常状态。

3.discardoldestpolicy:丢弃队列最前面的任务,然后重新提交被拒绝的任务。

4.callerrunspolicy:由调用线程处理该任务。

默认拒绝策略:

  /**
     * the default rejected execution handler
     */
    private static final rejectedexecutionhandler defaulthandler = new abortpolicy();
 
    public static class abortpolicy implements rejectedexecutionhandler {
        /**
         * creates an {@code abortpolicy}.
         */
        public abortpolicy() { }
 
        /**
         * always throws rejectedexecutionexception.
         *
         * @param r the runnable task requested to be executed
         * @param e the executor attempting to execute this task
         * @throws rejectedexecutionexception always
         */
        public void rejectedexecution(runnable r, threadpoolexecutor e) {
            throw new rejectedexecutionexception("task " + r.tostring() +
                                                 " rejected from " +
                                                 e.tostring());
        }
    }

自定义线程池工具

import java.util.concurrent.*;
import java.util.concurrent.atomic.atomicinteger;
 
/**
 * 线程池工厂工具
 *
 * @author 向振华
 * @date 2021/04/11 10:24
 */
public class threadpoolfactory {
 
    /**
     * 生成固定大小的线程池
     *
     * @param threadname 线程名称
     * @return 线程池
     */
    public static executorservice createfixedthreadpool(string threadname) {
        atomicinteger threadnumber = new atomicinteger(0);
        return new threadpoolexecutor(
                // 核心线程数
                desiredthreadnum(),
                // 最大线程数
                desiredthreadnum() * 2,
                // 空闲线程存活时间
                60l,
                // 空闲线程存活时间单位
                timeunit.seconds,
                // 工作队列
                new arrayblockingqueue<>(1024),
                // 线程工厂
                new threadfactory() {
                    @override
                    public thread newthread(runnable r) {
                        return new thread(r, threadname + "-" + threadnumber.getandincrement());
                    }
                },
                // 拒绝策略
                new rejectedexecutionhandler() {
                    @override
                    public void rejectedexecution(runnable r, threadpoolexecutor executor) {
                        if (!executor.isshutdown()) {
                            try {
                                //尝试阻塞式加入任务队列
                                executor.getqueue().put(r);
                            } catch (exception e) {
                                //保持线程的中断状态
                                thread.currentthread().interrupt();
                            }
                        }
                    }
                });
    }
 
    /**
     * 理想的线程数,使用 2倍cpu核心数
     */
    public static int desiredthreadnum() {
        return runtime.getruntime().availableprocessors() * 2;
    }
}

到此这篇关于java多线程之线程池七个参数详解的文章就介绍到这了,更多相关java线程池详解内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!