十三、ThreadPoolExecutor介绍

线程池各个参数含义

  • int corePoolSize, 核心线程数
  • int maximumPoolSize, 最大线程数
  • long keepAliveTime, 空闲线程存活时间
  • TimeUnit unit, 存活时间的单位
  • BlockingQueue workQueue, 存放线程任务队列
  • ThreadFactory threadFactory, 线程工厂,创建新线程
  • RejectedExecutionHandler handler 拒绝策略,线程池拒绝处理后的任务

注意:要执行的逻辑(Callable或者Runnable)是先交给workQueue,然后才是由线程池去指派线程执行workQueue中的任务,不是调用execute或者submit方法,就直接执行了。

关闭线程池的方法:在这里插入图片描述

package day09.part2;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 创建线程的开销是非常大的,为了更好的复用线程,
 * 提高效率。(就像高铁一样)
 *
 * 接口:Executor
 * 子接口:ExecutorService
 * 实现抽象类:AbstractExecutorService
 * 子类:ThreadPoolExecutor
 *
 * 线程池各个参数含义:
 *  int  corePoolSize,                     核心线程数
 *  int  maximumPoolSize,                  最大线程数
 *  long keepAliveTime,                    空闲线程存活时间
 *  TimeUnit unit,                         存活时间的单位
 *  BlockingQueue<Runnable> workQueue,     存放线程任务队列
 *  ThreadFactory threadFactory,           线程工厂,创建新线程
 *  RejectedExecutionHandler handler       拒绝策略,线程池拒绝处理后的任务
 *
 * 注意:要执行的逻辑(Callable或者Runnable)是先交给workQueue,
 * 然后才是由线程池去指派线程执行workQueue中的任务,不是调用execute或者submit方法
 * 就直接执行了。
 *
 *
 *
 * @author xzq
 */
public class ExecutorTest01 {

    public static void main(String[] args) {

        ThreadPoolExecutor threadPool = createThreadPool();

        int activeCount =-1;
        int queueSize = -1;
        while(true){
            if(activeCount!=threadPool.getActiveCount()||queueSize!=threadPool.getQueue().size()){
               System.out.println("线程池核心线程数是:"+threadPool.getCorePoolSize());
                int activeTempCount = threadPool.getActiveCount();
                int queueTempSize = threadPool.getQueue().size();
                  System.out.println("线程池活跃线程数是:"+activeTempCount);
                  System.out.println("线程池最大线程数是:"+threadPool.getMaximumPoolSize());
                  System.out.println("线程池阻塞任务队列大小是:"+queueTempSize);
                activeCount=activeTempCount;
                queueSize=queueTempSize;
            }
        }

    }


    /**
     * 创建线程池方法
     * @return
     */
    private static ThreadPoolExecutor createThreadPool() {
        //创建一个阻塞队列 可以设置队列的大小
        BlockingQueue<Runnable> blcQ=new ArrayBlockingQueue<>(1);
        //创建线程工厂
        ThreadFactory threadFactory=new MyThreadFactory();
        //创建拒绝策略
        RejectedExecutionHandler handler=new ThreadPoolExecutor.AbortPolicy();
        ThreadPoolExecutor threadPool=new ThreadPoolExecutor(
                1, 2, 5L, TimeUnit.SECONDS, blcQ, threadFactory, handler);

        System.out.println("线程池创建完毕---------------------------------");


        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                sleepSeconds(1);//改为30
            }
        });
        //-------------演示核心线程执行所有任务
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                sleepSeconds(1);
            }
        });
        //-------------演示新创建线程
        threadPool.execute(new Runnable() {
            @Override
            public void run() {
                sleepSeconds(1);
            }
        });
        /*
         * 休眠8秒  演示空闲超时时间,
         * 核心线程执行任务30秒,超时5秒,休眠8秒
         */
        /*try {
            Thread.sleep(8_000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
        //-------------演示执行拒绝策略
       /* threadPool.execute(new Runnable() {
            @Override
            public void run() {
                sleepSeconds(1);
            }
        });*/
        /*
         * 8秒后放两个任务进去,使它创建新的线程,
         * 观察与原有非核心线程是否是同一个线程
         */
        /*threadPool.execute(new Runnable() {
            @Override
            public void run() {
                sleepSeconds(1);
            }
        });*/


        return threadPool;
    }

    private static void sleepSeconds(int seconds){
        try {
            System.out.println(Thread.currentThread().getName()+"正在执行……");
            Thread.sleep(seconds*1000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    static class MyThreadFactory implements ThreadFactory{

        private AtomicInteger threadNum=new AtomicInteger();

        @Override
        public Thread newThread(Runnable r) {
            threadNum.getAndIncrement();//threadNum++
            return new Thread(r, "线程池中的线程"+threadNum+":");
//          return new Thread(r);
        }

    }

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值