线程池的一些问题

核心线程数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.场景三

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

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

相关推荐
小白学大数据3 分钟前
实战:Python爬虫如何模拟登录与维持会话状态
开发语言·爬虫·python
一念&5 分钟前
每日一个C语言知识:C 结构体
c语言·开发语言
Chen-Edward12 分钟前
有了Spring为什么还有要Spring Boot?
java·spring boot·spring
锦***林43 分钟前
用 Python 写一个自动化办公小助手
开发语言·python·自动化
陈小桔1 小时前
idea中重新加载所有maven项目失败,但maven compile成功
java·maven
小学鸡!1 小时前
Spring Boot实现日志链路追踪
java·spring boot·后端
xiaogg36781 小时前
阿里云k8s1.33部署yaml和dockerfile配置文件
java·linux·kubernetes
逆光的July2 小时前
Hikari连接池
java
微风粼粼2 小时前
eclipse 导入javaweb项目,以及配置教程(傻瓜式教学)
java·ide·eclipse
番茄Salad2 小时前
Spring Boot临时解决循环依赖注入问题
java·spring boot·spring cloud