Java  手动创建线程池


jdk提供了一个通过ThreadPoolExecutor创建一个线程池的类

构造器

使用给定的参数和默认的饱和策略、默认的工厂方法创建线程池

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

使用给定的参数和默认的工厂方法创建线程池

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

使用给定的参数和默认的饱和策略(AbortPolicy)创建线程池

ThreadPoolExecutor(int corePoolSize, 
int maximumPoolSize,
long keepAliveTime,
BlockingQueue<Runnable> workQueue, 
ThreadFactory threadFactory)

使用指定的参数创建线程池

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

参数说明

  • corePoolSize 线程池的基本大小, 当提交一个任务到线程池的时候,线程池会创建一个线程来执行任务,即使当前线程池已经存在空闲线程,仍然会创建一个线程,等到需要执行的任务数大于线程池基本大小时就不再创建。如果调用线程池的prestartAllCoreThreads()方法,线程池会提前创建并启动所有的基本线程。
  • maximumPoolSizeSize 线程池最大数量,线程池允许创建的最大线程数,如果队列满了,并且已创建的线程数小于最大线程数,则线程池会再创建新的线程执行任务。值得注意的是,如果使用了无界的任务队列这个参数就没什么效果。
  • keepAliveTime 线程活动保持时间,线程池的工作线程空闲后,保持存活的时间,所以,如果任务很多,并且每个任务执行的时间比较短,可以调大时间,提高线程的利用率。
  • unit 线程活动保持时间的单位,可选择的单位有时分秒等等。
  • workQueue 任务队列。用来暂时保存任务的工作队列
  • threadFactory 用于创建线程的工厂

队列

  • ArrayBlockingQueue:是一个基于数组结构的有界阻塞队列,此队列按照FIFO(先进先出)原则对元素进行排序
  • DelayQueue
  • LinkedBlockingDeque
  • LinkedBlockingQueue:是一个基于链表结构的有界阻塞队列,此队列按照FIFO排序元素,吞吐量高于ArrayBlockingQueue。静态工厂方法Executors.newFixedThreadPool(n)使用了此队列
  • LinkedTransferQueue
  • PriorityBlockingQueue:一个具有优先级的无限阻塞队列
  • SynchronousQueue:一个不存储元素的阻塞队列。每个插入操作必须等待另一个线程调用移除操作,否则插入操作一直处于阻塞状态,吞吐量要高于LinkedBlockingQueue,静态工厂方法Executors.newCachedThreadPool()使用了此队列

饱和策略

当队列和线程池都满了,说明线程池处于饱和的状态,那么必须采取一种策略处理提交的新任务。这个策略默认是AbortPolicy,表示无法处理新任务时抛出异常

  • ThreadPoolExecutor.AbortPolicy:直接抛出异常
  • ThreadPoolExecutor.CallerRunsPolicy:只用调用这所在的线程来运行任务
  • ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列里最近的一个任务,并执行当前任务
  • ThreadPoolExecutor.DiscardPolicy:不处理,丢弃掉

示例

public class ThreadPool {

    /**
     * 线程池的基本大小
     */
    static int corePoolSize = 10;
    /**
     * 线程池最大数量
     */
    static int maximumPoolSizeSize = 100;
    /**
     * 线程活动保持时间
     */
    static long keepAliveTime = 1;
    /**
     * 任务队列
     */
    static ArrayBlockingQueue workQueue = new ArrayBlockingQueue(10);

    public static void main(String[] args) {
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                corePoolSize,
                maximumPoolSizeSize,
                keepAliveTime,
                TimeUnit.SECONDS,
                workQueue,
                new ThreadFactoryBuilder().setNameFormat("XX-task-%d").build());
        //提交一个任务
        executor.execute(() -> System.out.println("ok"));
    }
}

源码分析

任务执行

public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
        /*
         * Proceed in 3 steps:
         *
         * 1. If fewer than corePoolSize threads are running, try to
         * start a new thread with the given command as its first
         * task.  The call to addWorker atomically checks runState and
         * workerCount, and so prevents false alarms that would add
         * threads when it shouldn't, by returning false.
         *
         * 2. If a task can be successfully queued, then we still need
         * to double-check whether we should have added a thread
         * (because existing ones died since last checking) or that
         * the pool shut down since entry into this method. So we
         * recheck state and if necessary roll back the enqueuing if
         * stopped, or start a new thread if there are none.
         *
         * 3. If we cannot queue task, then we try to add a new
         * thread.  If it fails, we know we are shut down or saturated
         * and so reject the task.
         */
        int c = ctl.get();
        if (workerCountOf(c) < corePoolSize) {
            if (addWorker(command, true))
                return;
            c = ctl.get();
        }
        if (isRunning(c) && workQueue.offer(command)) {
            int recheck = ctl.get();
            if (! isRunning(recheck) && remove(command))
                reject(command);
            else if (workerCountOf(recheck) == 0)
                addWorker(null, false);
        }
        else if (!addWorker(command, false))
            reject(command);
    }

参考文档

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html

  • 5
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值