展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

线程池的核心组件及运行机制

  • 简介
corePoolSize:核心线程池大小 cSize
maximumPoolSize:线程池最大容量 mSize
keepAliveTime:当线程数量大于核心时,多余的空闲线程在终止之前等待新任务的最大时间
unit:时间单位
workQueue: 工作队列 nWorks
ThreadFactory:线程工厂
handler:拒绝策略
运行机制如下:
    通过new创建线程池时,除非调用prestartAllCoreThreads方法初始化核心线程,否则此时线程池中有0个线程,即使工作队列中存在多个任务,同样
    不会执行
  • 案例1
public class ThreadPoolDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建队列,队列大小指定20
        LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>();
        // 往线程池中放入线程
        for (int i = 0; i <100 ; i++) {
            objects.put(()->{
                System.out.println(Thread.currentThread().getName());
            });
        }
        // 创建线程池,每次处理10个任务,3秒
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L,
                TimeUnit.SECONDS, objects);
    }

}

# 控制台:不会执行
  • 案例2
public class ThreadPoolDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建队列,队列大小指定20
        LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>();
        // 往线程池中放入线程
        for (int i = 0; i <100 ; i++) {
            objects.put(()->{
                System.out.println(Thread.currentThread().getName());
            });
        }
        // 创建线程池,每次处理10个任务,3秒
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L,
                TimeUnit.SECONDS, objects);
        // 初始化核心线程
        threadPoolExecutor.prestartAllCoreThreads();
    }

}

# 控制台打印成功
  • 结论
任务数X
    x <= cSize 只启动x个线程
    x >= cSize && x < nWorks + cSize 会启动 <= cSize 个线程 其他的任务就放到工作队列里
    x > cSize && x > nWorks + cSize
        x-(nWorks) <= mSize 会启动x-(nWorks)个线程 --> 当启动的任务数-队列容量小于线程池最大容量时
        x-(nWorks) > mSize 会启动mSize个线程来执行任务,其余的执行相应的拒绝策略 --> 当启动的任务数-队列容量大于线程池最大容量时
  • 案例3:启动的任务数量小于核心线程数时
public class ThreadPoolDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建队列,容量不限制
        LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>();
        // 创建线程池,每次处理10个任务,3秒
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L,
                TimeUnit.SECONDS, objects);
        // 初始化核心线程
        threadPoolExecutor.prestartAllCoreThreads();
        // 执行5个线程
        for (int i = 0; i < 5; i++) {
            threadPoolExecutor.submit(()->{         // 提交任务
                System.out.println(threadPoolExecutor.getActiveCount());    // 获取线程池中活跃的数量
            });
        }
    }

}

# 控制台打印结果:有5个活跃的任务
5
5
5
5
5
  • 案例4:启动的任务数量大于核心线程数,小于工作队列数+核心线程数
public class ThreadPoolDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        // 创建队列,队列大小指定20
        LinkedBlockingQueue<Runnable> objects = new LinkedBlockingQueue<>(20);
        // 创建线程池,每次处理10个任务,3秒
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(10, 20, 3L,
                TimeUnit.SECONDS, objects);
        // 初始化核心线程
        threadPoolExecutor.prestartAllCoreThreads();
        // 执行20个线程
        for (int i = 0; i < 20; i++) {
            threadPoolExecutor.submit(()->{         // 提交任务
                try {
                    Thread.sleep(2000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(threadPoolExecutor.getActiveCount());    // 获取线程池中活跃的数量
            });
        }
    }

}

# 控制台打印如下:
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
9
9
9
9
posted @ 2022-05-16 15:46  DogLeftover  阅读(48)  评论(0编辑  收藏  举报