线程池监控

package org.example.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;

/**
 * Description: 线程池工厂
 */
@Slf4j
public class ThreadPoolFactory {

    /**
     * 线程池1
     */
    private static ThreadPoolProxy demoF = new ThreadPoolProxy(10, "demo1");

    /**
     * 线程池2
     */
    private static ThreadPoolProxy demoT = new ThreadPoolProxy(4, "demo2");


    static {
        //ThreadFactory threadFactory = new ThreadPoolProxy.DemoThreadFactory("scheduledPool");
        //ScheduledExecutorService scheduledExecutorService = new ScheduledThreadPoolExecutor(1, threadFactory);
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.scheduleAtFixedRate(() -> {
            log.info("ThreadPoolFactory 线程池队列监控开始");
            log.info("线程池1队列堵塞:{}", getUploadBdaThreadPool().getBlockSize());
            log.info("线程池2队列堵塞:{}", getUploadSensorThreadPool().getBlockSize());
            log.info("ThreadPoolFactory 线程池队列监控结束");
        }, 10L, 20L, TimeUnit.SECONDS);
    }

    /**
     * 线程池1
     *
     * @return 线程池
     */
    public static ThreadPoolProxy getUploadBdaThreadPool() {
        if (demoF == null) {
            synchronized (ThreadPoolProxy.class) {
                if (demoF == null) {
                    demoF = new ThreadPoolProxy(10, "uploadBda");
                }
            }
        }
        return demoF;
    }

    /**
     * 线程池2
     *
     * @return 线程池
     */
    public static ThreadPoolProxy getUploadSensorThreadPool() {
        if (demoT == null) {
            synchronized (ThreadPoolProxy.class) {
                if (demoT == null) {
                    demoT = new ThreadPoolProxy(3, "uploadSensor");
                }
            }
        }
        return demoT;
    }

}
package org.example.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Description: 线程池代理
 */
@Slf4j
public class ThreadPoolProxy {

    private ThreadPoolExecutor threadPoolExecutor;
    private int corePoolSize;
    private String threadPoolNamePrefix;

    /**
     * 构造函数
     *
     * @param corePoolSize 核心线程数
     */
    public ThreadPoolProxy(int corePoolSize, String threadPoolNamePrefix) {
        this.corePoolSize = corePoolSize;
        this.threadPoolNamePrefix = threadPoolNamePrefix;
    }

    /**
     * 初始化线程池
     * 双重检查加锁
     */
    private void initThreadPoolExecutor() {
        if (threadPoolExecutor == null) {
            synchronized (ThreadPoolProxy.class) {
                if (threadPoolExecutor == null) {
                    BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
                    ThreadFactory threadFactory = new DemoThreadFactory(threadPoolNamePrefix);
                    RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
                    this.threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, Integer.MAX_VALUE, 60,
                            TimeUnit.SECONDS, workQueue, threadFactory,
                            handler);
                }
            }
        }
    }

    /**
     * 执行任务
     */
    public void execute(Runnable task) {
        initThreadPoolExecutor();
        threadPoolExecutor.execute(task);
    }

    /**
     * 提交任务
     */
    public Future<?> submit(Callable<?> task) {
        initThreadPoolExecutor();
        return threadPoolExecutor.submit(task);
    }

    /**
     * 提交任务
     */
    public Future<Integer> submitInt(Callable<Integer> task) {
        initThreadPoolExecutor();
        return threadPoolExecutor.submit(task);
    }

    long getBlockSize() {
        initThreadPoolExecutor();
        return threadPoolExecutor.getQueue().size();
    }

    /**
     * 线程工厂
     */
    public static class DemoThreadFactory implements ThreadFactory {

        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        public DemoThreadFactory(String threadNamePrefix) {
            SecurityManager s = System.getSecurityManager();
            group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
            namePrefix = threadNamePrefix + "-threadPool-";
        }

        @Override
        public Thread newThread(Runnable r) {
            Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
            if (t.isDaemon()) {
                t.setDaemon(false);
            }
            if (t.getPriority() != Thread.NORM_PRIORITY) {
                t.setPriority(Thread.NORM_PRIORITY);
            }
            t.setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

                @Override
                public void uncaughtException(Thread t, Throwable e) {
                    log.error("thread:{}===>Exception:{}", t.getName(), e.getMessage());
                    log.error("线程异常:", e);
                    throw new RuntimeException(e);
                }
            });
            return t;
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值