线程池的一些问题

发布于:2024-05-16 ⋅ 阅读:(52) ⋅ 点赞:(0)

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

1.场景一

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

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


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.场景三

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

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