java线程数,基于全面监控指标的自适应调整

import java.lang.management.ManagementFactory;
import java.lang.management.OperatingSystemMXBean;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
 
public class EnhancedAdaptiveThreadPool {
 
    private final ThreadPoolExecutor executor;
    private final ScheduledExecutorService monitor;
 
    // 线程池配置参数
    private final int minCoreSize = 2;
    private final int maxCoreSize;
    private final int queueSizeThreshold = 20; // 队列长度阈值
 
    // 任务性能跟踪
    private final ConcurrentHashMap<Long, Long> taskStartTimes = new ConcurrentHashMap<>();
    private final AtomicInteger totalTasks = new AtomicInteger(0);
    private final AtomicInteger completedTasks = new AtomicInteger(0);
    private double avgProcessingTime = 0;
 
    public EnhancedAdaptiveThreadPool() {
        // 根据CPU核心数确定最大核心线程数
        int cpuCores = Runtime.getRuntime().availableProcessors();
        this.maxCoreSize = cpuCores * 2; // 一般建议线程数为CPU核心数的1-2倍
 
        System.out.println("系统CPU核心数:" + cpuCores + ", 设置最大核心线程数:" + maxCoreSize);
 
        // 创建主线程池
        this.executor = new ThreadPoolExecutor(
                minCoreSize,
                maxCoreSize * 2, // 最大线程数设为核心线程数的2倍
                60, TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(100),
                new CustomThreadFactory("自适应线程"),
                new ThreadPoolExecutor.CallerRunsPolicy());
 
        // 创建监控线程
        this.monitor = Executors.newScheduledThreadPool(1);
 
        // 启动自动调整
        startMonitoring();
    }
 
    private static class CustomThreadFactory implements ThreadFactory {
        private final AtomicInteger counter = new AtomicInteger(1);
        private final String prefix;
 
        public CustomThreadFactory(String prefix) {
            this.prefix = prefix;
        }
 
        @Override
        public Thread newThread(Runnable r) {
            Thread thread = new Thread(r, prefix + "-" + counter.getAndIncrement());
            // 设置为非守护线程
            thread.setDaemon(false);
            // 设置为正常优先级
            thread.setPriority(Thread.NORM_PRIORITY);
            return thread;
        }
    }
 
    public <T> Future<T> submit(Callable<T> task) {
        // 包装任务以跟踪性能
        return executor.submit(new Callable<T>() {
            @Override
            public T call() throws Exception {
                long startTime = System.currentTimeMillis();
                long threadId = Thread.currentThread().getId();
                taskStartTimes.put(threadId, startTime);
                totalTasks.incrementAndGet();
 
                try {
                    return task.call();
                } finally {
                    long endTime = System.currentTimeMillis();
                    taskStartTimes.remove(threadId);
                    completedTasks.incrementAndGet();
 
                    // 更新平均处理时间(简单滑动平均)
                    double processingTime = endTime - startTime;
                    avgProcessingTime = (avgProcessingTime * 0.8) + (processingTime * 0.2);
                }
            }
        });
    }
 
    private void startMonitoring() {
        // 每5秒检查一次负载并调整
        monitor.scheduleAtFixedRate(() -> {
            // 获取线程池指标
            int queueSize = executor.getQueue().size();
            int currentCoreSize = executor.getCorePoolSize();
            int activeThreads = executor.getActiveCount();
            int poolSize = executor.getPoolSize();
 
            // 获取系统CPU使用率
            double cpuLoad = getSystemCpuLoad();
 
            // 任务吞吐量计算
            int completed = completedTasks.get();
            double throughput = completed / 5.0; // 每秒完成任务数
 
            // 重置计数器
            completedTasks.set(0);
 
            System.out.println("== 监控报告 ==");
            System.out.println("CPU使用率: " + String.format("%.2f", cpuLoad * 100) + "%");
            System.out.println("队列长度: " + queueSize);
            System.out.println("当前核心线程数: " + currentCoreSize);
            System.out.println("当前线程池大小: " + poolSize);
            System.out.println("活跃线程数: " + activeThreads);
            System.out.println("任务吞吐量: " + String.format("%.2f", throughput) + "任务/秒");
            System.out.println("平均处理时间: " + String.format("%.2f", avgProcessingTime) + "ms");
 
            // 计算理想线程数
            // Little's Law: 并发数 = 吞吐量 × 响应时间
            int idealThreadCount = Math.max(minCoreSize, (int) Math.ceil(throughput * (avgProcessingTime / 1000.0)));
            idealThreadCount = Math.min(idealThreadCount, maxCoreSize);
            // 基于多种指标综合决策
            boolean highLoad = queueSize > queueSizeThreshold ||
                              (activeThreads >= currentCoreSize * 0.8) ||
                              (cpuLoad < 0.7 && throughput > currentCoreSize);
            boolean lowLoad = queueSize == 0 &&
                             activeThreads < currentCoreSize * 0.3 &&
                             currentCoreSize > minCoreSize &&
                             cpuLoad < 0.3;
            if (highLoad) {
                // 负载高,需要增加核心线程
                int newCoreSize = Math.min(currentCoreSize + 1, maxCoreSize);
                if (newCoreSize > currentCoreSize) {
                    System.out.println("⚠️ 检测到高负载,增加核心线程数: " + currentCoreSize + " -> " + newCoreSize);
                    executor.setCorePoolSize(newCoreSize);
                }
            } else if (lowLoad) {
                // 负载低,可以减少核心线程
                int newCoreSize = Math.max(currentCoreSize - 1, minCoreSize);
                if (newCoreSize < currentCoreSize) {
                    System.out.println("⚠️ 检测到低负载,减少核心线程数: " + currentCoreSize + " -> " + newCoreSize);
                    executor.setCorePoolSize(newCoreSize);
                }
            } else if (Math.abs(currentCoreSize - idealThreadCount) > 2) {
                // 根据Little's Law调整到理想线程数
                System.out.println("⚠️ 根据性能指标调整,核心线程数: " + currentCoreSize + " -> " + idealThreadCount);
                executor.setCorePoolSize(idealThreadCount);
            }
        }, 5, 5, TimeUnit.SECONDS);
    }
 
    private double getSystemCpuLoad() {
        OperatingSystemMXBean osBean = ManagementFactory.getOperatingSystemMXBean();
        // 这里使用系统平均负载作为参考
        // 在生产环境中,可以使用JMX或其他方法获取更精确的CPU使用率
        double loadAvg = osBean.getSystemLoadAverage();
        if (loadAvg < 0) return 0.5; // 某些系统可能不支持此方法
        return Math.min(loadAvg / Runtime.getRuntime().availableProcessors(), 1.0);
    }
 
    public ThreadPoolExecutor getExecutor() {
        return executor;
    }
 
    public void shutdown() {
        monitor.shutdown();
        executor.shutdown();
    }
}

 

posted on 2025-08-23 12:32  yebinghuai-qq-com  阅读(19)  评论(0)    收藏  举报

导航