首页 > 其他分享 >线程池监控2-监控线程池状态、线程数量和队列任务数量等

线程池监控2-监控线程池状态、线程数量和队列任务数量等

时间:2024-10-10 12:11:01浏览次数:1  
标签:poolName 数量 GlobalMonitor threadPoolMonitor 线程 监控 new public

1.实现原理

  这篇博文是基于线程池监控1-监控任务执行时间,原理是:创建一个固定时间间隔执行的线程,来记录线程池的池状态、线程数量和队列任务数量等,具体方案:使用单例类缓存所有创建的线程池对象,类创建时启动定时任务线程,定期遍历缓存中线程池,记录线程池信息。

2.实现代码

package com.xkzhangsan.thread.pool.monitor;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 全局监控 <br>
 * 1.定期记录线程池基本信息 <br>
 *
 * @author xkzhangsan
 */
public class GlobalMonitor {
    private static volatile GlobalMonitor instance;
    private static final ConcurrentHashMap<String, ThreadPoolMonitor> threadPoolMonitorMap = new ConcurrentHashMap<>();
    private static final ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);

    private GlobalMonitor() {
        scheduledThreadPoolExecutor.scheduleAtFixedRate(new PoolInfoRunnable(), 10, 10, TimeUnit.SECONDS);
    }

    public static GlobalMonitor getInstance() {
        if (instance == null) {
            synchronized (GlobalMonitor.class) {
                if (instance == null) {
                    instance = new GlobalMonitor();
                }
            }
        }
        return instance;
    }

    public void put(String poolName, ThreadPoolMonitor threadPoolMonitor) {
        threadPoolMonitorMap.put(poolName, threadPoolMonitor);
    }

    public void remove(String poolName) {
        threadPoolMonitorMap.remove(poolName);
    }

    static class PoolInfoRunnable implements Runnable {

        @Override
        public void run() {
            threadPoolMonitorMap.forEach((poolName, threadPoolMonitor) -> {
                int currentPoolSize = threadPoolMonitor.getPoolSize();
                int queueSize = threadPoolMonitor.getQueue().size();
                System.out.println("poolName:" + poolName + " status:" + threadPoolMonitor.getStatus() + " corePoolSize:" + threadPoolMonitor.getCorePoolSize() + " maximumPoolSize:"
                        + threadPoolMonitor.getMaximumPoolSize() + " currentPoolSize:" + currentPoolSize + " queueCapacity:" + threadPoolMonitor.getQueueCapacity()
                        + " queueSize:" + queueSize);
            });
        }
    }

}

 

获取线程池状态

这里参考了ThreadPoolExecutor的toString(),返回Running、Shutting down、Terminated 三种状态。

见:ThreadPoolMonitor类

    public String getStatus() {
        if (super.isTerminated()) {
            return "Terminated";
        } else if (super.isShutdown()) {
            return "Shutting down";
        } else {
            return "Running";
        }
    }

获取队列总容量

创建ThreadPoolExecutor时,传入的BlockingQueue<Runnable> workQueue,无法直接获取总容量,ThreadPoolExecutor又没有直接获取总容量的方法,
这里想到另一个方法,Queue的remainingCapacity()返回当前队列剩余容量,原理:总容量-队列size,所以,在刚创建时size为0,返回的就时总容量。
见:ThreadPoolMonitor类

    private void init(String poolName, MonitorLevelEnum monitorLevel) {
        this.poolName = poolName;
        this.monitorLevel = monitorLevel;
        this.taskStartTimeMap = new ConcurrentHashMap<>();
        if (isPoolMonitor()) {
            GlobalMonitor.getInstance().put(poolName, this);
        }
        this.queueCapacity = super.getQueue().remainingCapacity();
    }

    public int getQueueCapacity() {
        return this.queueCapacity;
    }


3.测试运行

3.1 测试代码

package com.xkzhangsan.thread.pool.monitor;

import com.xkzhangsan.thread.pool.monitor.constant.MonitorLevelEnum;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class ThreadPoolMonitorTest {

    public static void main(String[] args) {
        poolMonitor();
    }

    public static void poolMonitor() {
        ThreadPoolMonitor threadPoolMonitor = new ThreadPoolMonitor(1, 3, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(1000), "test", MonitorLevelEnum.POOL);
        for (int i = 0; i < 100; i++) {
            int finalI = i;
            threadPoolMonitor.execute(() -> {
                try {
                    TimeUnit.SECONDS.sleep(3);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                System.out.println(finalI);
            });
        }
        threadPoolMonitor.shutdown();
    }
}

 

3.2 测试结果

0
1
2
poolName:test status:Shutting down corePoolSize:1 maximumPoolSize:3 currentPoolSize:1 queueCapacity:1000 queueSize:96
3
4
5
poolName:test status:Shutting down corePoolSize:1 maximumPoolSize:3 currentPoolSize:1 queueCapacity:1000 queueSize:93
6
7
8
poolName:test status:Shutting down corePoolSize:1 maximumPoolSize:3 currentPoolSize:1 queueCapacity:1000 queueSize:90
9
线程sleep 3s,监控日志10s打印一次,队列中的任务在不断减少。
源代码地址:https://github.com/xkzhangsan/thread-pool-monitor


标签:poolName,数量,GlobalMonitor,threadPoolMonitor,线程,监控,new,public
From: https://www.cnblogs.com/xkzhangsanx/p/18456054

相关文章

  • 通用线程池1
    1publicclassCommonThreadPool{2privatestaticfinalCommonThreadPoolpool=newCommonThreadPool();3privateThreadPoolExecutorexecutor;4privateCommonThreadPool(){5intcorePoolSize=4;6intmaximumPoolSize......
  • 掌握Linux性能监控神器:atop实用指南
    在Linux系统管理中,性能监控是确保系统运行平稳的重要环节。atop是一款强大的性能监控工具,可以提供详细的系统性能数据,包括CPU、内存、磁盘和网络的使用情况。本文将介绍如何安装、配置和使用atop来监控Linux系统的性能。安装atop在绝大多数Linux发行版本中,atop工具能够便......
  • 视频汇聚平台EasyCVR支持云端录像丨监控存储丨录像回看丨录像计划丨录像配置
    EasyCVR视频汇聚融合平台,是TSINGSEE青犀视频垂直深耕音视频流媒体技术、AI智能技术领域的杰出成果。平台以其强大的视频处理、汇聚与融合能力,在构建视频监控系统中展现出了独特的优势。EasyCVR视频汇聚平台可接入传统监控行业中高清网络摄像机的RTSP直播流,及RTMP、HTTP-FLV、HLS(......
  • 非煤矿山监控预警系统
    非煤矿山监控预警系统通过在矿山内部布设监控摄像头,非煤矿山监控预警系统实时监测人员的作业行为和环境状况。非煤矿山监控预警系统利用图像识别技术和传感器数据,对人员的作业行为、现场车辆运输设备等作业状态进行分析和识别。非煤矿山监控预警系统通过图像识别和传感器数据分析,......
  • C#使用线程安全队列ConcurrentQueue处理数据
    usingSystem;usingSystem.Collections.Concurrent;usingSystem.Collections.Generic;usingSystem.Globalization;usingSystem.Linq;usingSystem.Text;usingSystem.Threading;usingSystem.Threading.Tasks;namespaceConsoleApp10{internalclassProg......
  • AI+视频监控:EasyCVR安防平台赋能火电制造行业的视频智能管理方案
    随着信息技术的飞速发展和智能制造的深入推进,火电制造行业作为国民经济的重要组成部分,正面临着智能化转型的迫切需求。为了提升生产效率、保障设备安全、优化管理流程,火电制造企业迫切需要引入先进的视频监控与人工智能技术。EasyCVR安防监控平台凭借其强大的视频汇聚、智能分析和......
  • 算法题:ID数量
    目录题目描述示例Java解答注意在何处取余不影响结果正确性?对乘法和加法的运算表达式里取余都不会,因为有取模运算的分配律(a*b)modm=[(amodm)*(bmodm)]modm(a*b)modm=[(amodm)*(bmodm)]modm题目描述大学生小名设计了一种语言,他的语言最大支持......
  • Docker 环境下 GPU 监控实战:使用 Prometheus 实现 DCGM Exporter 部署与 GPU 性能监控
    Docker环境下GPU监控实战:使用Prometheus实现DCGMExporter部署与GPU性能监控文章目录Docker环境下GPU监控实战:使用Prometheus实现DCGMExporter部署与GPU性能监控一查看当前GPU信息二dcgm-exporter部署1)Dockerrun运行2)Dockercompose运行三......
  • 基于爬虫与文本挖掘的网络舆情监控系统python
    本网络舆情监控系统基于Java与SpringBoot技术,结合爬虫与文本挖掘功能,旨在高效地监测和分析网络舆情。系统设计上注重高效性与准确性。Java语言提供了稳定的基础开发环境,确保系统的可靠运行。SpringBoot框架使得系统易于构建和扩展,能快速集成各种相关组件。利用爬......
  • 液体泄露检测系统 监控识别管道液体泄漏系统
    液体泄露检测系统通过在关键区域安装监控摄像头,液体泄露检测系统对管道的液体泄露情况进行全天候不间断实时监测。液体泄露检测系统利用Ai视觉智能分析技术,实时感知监控画面中管道液体泄露事件。液体泄露检测系统检测到画面中管道设备液体泄露现象时,将自动发出警报提示相关人员及......