线程池的一些问题

核心线程数1.最大线程5.队列5.存活时间10s

1.场景一

复制代码
如果核心线程数.被一直占用得不到释放.新进来1个任务.会怎么样?

答: 会在队列中中死等. 只要进来的任务.不超过队列的长度,就会一直挡在队列中死等

java 复制代码
package com.lin;

import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @author lin
 */
public class ThreadPoolExample {

    public static void main(String[] args) {
        // 创建线程池
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
            1,                        // 核心线程数
            5,                        // 最大线程数
            1,                        // 空闲线程存活时间
            TimeUnit.MINUTES,         // 存活时间的单位
            new LinkedBlockingQueue<Runnable>(6)  // 任务队列
        );

        // 提交第一个永不释放的任务给线程池
        threadPoolExecutor.execute(() -> {
            try {
                System.out.println("Task 1 is running and will never complete");
                while (true) {
                    // 模拟一个永不释放的任务
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                System.out.println("Task 1 was interrupted");
                Thread.currentThread().interrupt();
            }
        });

        // 等待一段时间然后提交第二个任务
        try {
            System.out.println("等待2秒");
            Thread.sleep(2000); // 等待2秒,确保第一个任务已开始执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        for (int i = 0; i < 6; i++) {   //模拟任务数量
            int finalI = i;
            threadPoolExecutor.execute(() -> {
                try {
                    System.out.println("Task "+ finalI +"is running");
                    Thread.sleep(1000); // 模拟任务运行
                    System.out.println("Task "+ finalI +"is completed");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });


        }



        // 打印线程池状态
        new Thread(() -> {
            while (true) {
                try {
                    System.out.println("Active Threads: " + threadPoolExecutor.getActiveCount());
                    System.out.println("Pool Size: " + threadPoolExecutor.getPoolSize());
                    System.out.println("Queue Size: " + threadPoolExecutor.getQueue().size());
                    Thread.sleep(5000); // 每5秒打印一次线程池状态
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();
                    break;
                }
            }
        }).start();

        // 等待足够的时间观察线程池状态
        //try {
        //    Thread.sleep(60000); // 主线程等待60秒
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        //
         关闭线程池
        //threadPoolExecutor.shutdownNow();
        //try {
        //    // 等待所有任务完成
        //    if (!threadPoolExecutor.awaitTermination(60, TimeUnit.SECONDS)) {
        //        threadPoolExecutor.shutdownNow();
        //    }
        //} catch (InterruptedException e) {
        //    threadPoolExecutor.shutdownNow();
        //}
    }
}

2.场景二

复制代码
如果核心线程数,一直被占用.来了6个任务.效果会怎么样

答: 这是任务总数超过了队列的长度.线程池会创建新的线程来处理这个任务.根据队列的不同.消费任务的顺序不一样.以LinkedBlockingQueue.1,2,3,4,5添加到队列中.第6个任务来了.会创建线程先消费. 然后再又这个线程来处理1,2,3,4,5来处理.

然后处理完.空闲线程等待存活时间.然后被回收.线程池恢复到最开始的时候.


3.场景三

复制代码
如果核心线程数和最大线程数的其他线程同时空置了,最大线程数的其他线程还没有被回收.现在队列中的任务,会被那个消费

答: 会优先被核心线程先消费

相关推荐
FrankYoou1 小时前
Jenkins 与 GitLab CI/CD 的核心对比
java·docker
麦兜*1 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
Coding小公仔2 小时前
C++ bitset 模板类
开发语言·c++
KK溜了溜了2 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
小赖同学啊2 小时前
物联网数据安全区块链服务
开发语言·python·区块链
天河归来2 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
shimly1234562 小时前
bash 脚本比较 100 个程序运行时间,精确到毫秒,脚本
开发语言·chrome·bash
weixin_478689762 小时前
十大排序算法汇总
java·算法·排序算法
码荼2 小时前
学习开发之hashmap
java·python·学习·哈希算法·个人开发·小白学开发·不花钱不花时间crud