方法 | 描述 | 监控方面 |
---|---|---|
getActiveCount() | 获取正在工作的线程数 | 监控线程的变化 |
getPoolSize() | 获取当前存在的线程数 | 监控线程的变化 |
getLargestPoolSize() | 获取历史最大的线程数 | 监控线程的变化 |
getTaskCount() | 获取计划执行的任务总数 | 监控任务的变化 |
getCompletedTaskCount() | 获取已完成的任务数 | 监控任务的变化 |
getQueue() | 获取任务队列 | 监控任务的变化 |
package com.kang.mongodb.pool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import java.util.concurrent.*;
/**
* @Author Emperor Kang
* @ClassName MonitorThhreadPool
* @Description TODO
* @Date 2023/8/7 13:44
* @Version 1.0
* @Motto 让营地比你来时更干净
*/
@Slf4j
public class MonitorThreadPool extends ThreadPoolExecutor {
/**
* 自定义线程池
* @param corePoolSize
* @param maximumPoolSize
* @param keepAliveTime
* @param unit
* @param workQueue
*/
public MonitorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
}
public MonitorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory);
}
public MonitorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
}
public MonitorThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler);
}
/**
* 重写before executor方法,该方法每次任务执行前调用,在他内部调用一遍monitor方法,每当有任务执行的时候,输出一次线程池的情况,
* @param t
* @param r
*/
@Override
protected void beforeExecute(Thread t, Runnable r) {
log.info("beforeExecute");
monitor();
}
/**
* 接着重写afterexecutor方法,该方法每次任务完成后调用,在它内部也调用一遍monitor方法,每当有任务完成的时候,输出一次线程池的情况,
* @param r
* @param t
*/
@Override
protected void afterExecute(Runnable r, Throwable t) {
log.info("afterExecute");
monitor();
}
/**
* 最后重写terminated的方法。该方法在线程池关闭前调用,同样的,在它的内部也调用一遍monitor方法,
*/
@Override
protected void terminated() {
log.info("terminated");
monitor();
}
/**
* 监控线程池情况
*/
public void monitor(){
log.info("正在工作的线程数:{}",getActiveCount());
log.info("当前存在的线程数:{}",getPoolSize());
log.info("历史最大的线程数:{}",getLargestPoolSize());
log.info("已提交的任务总数:{}",getTaskCount());
log.info("已完成的任务数:{}",getCompletedTaskCount());
log.info("队列中的任务数:{}",getQueue().size());
log.info("================线程池华丽分割线================");
}
}
private ThreadFactory springThreadFactory = new CustomizableThreadFactory("springThread-pool-");
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
10,
30,
5,
TimeUnit.MINUTES,
new ArrayBlockingQueue<Runnable>(1000),
springThreadFactory ); //给线程池中的线程自定义名称
private ThreadFactory guavaThreadFactory = new ThreadFactoryBuilder().setNameFormat("retryClient-pool-").build();
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
10,
30,
5,
TimeUnit.MINUTES,
new ArrayBlockingQueue<Runnable>(1000),
guavaThreadFactory ); //给线程池中的线程自定义名称
private ThreadFactory basicThreadFactory = new BasicThreadFactory.Builder().namingPattern("basicThreadFactory-").build();
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
10,
30,
5,
TimeUnit.MINUTES,
new ArrayBlockingQueue<Runnable>(1000),
basicThreadFactory ); //给线程池中的线程自定义名称
public class NamesThreadFactory implements ThreadFactory{
private final ThreadGroup group;
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
public NamesThreadFactory(String name) {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
if (null == name || name.isEmpty()) {
name = "pool";
}
namePrefix = name + "-thread-";
}
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);
return t;
}
}
使用方式
private NamesThreadFactory namesThreadFactory = new NamesThreadFactory("namesThread-");
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(
10,
30,
5,
TimeUnit.MINUTES,
new ArrayBlockingQueue<Runnable>(1000),
namesThreadFactory); //给线程池中的线程自定义名称
package com.kang.mongodb.pool;
import lombok.extern.slf4j.Slf4j;
/**
* @Author Emperor Kang
* @ClassName Task
* @Description 制定一个任务task,实现runnable接口,定义一个int类型的变量timeout,表示任务执行时长,重载构造方法用于初始化timeout,任务内容是使当前线程休眠,以此来模拟任务执行时长。
* @Date 2023/8/7 14:07
* @Version 1.0
* @Motto 让营地比你来时更干净
*/
@Slf4j
public class Task implements Runnable{
/**
* 执行时间
*/
private int timeout;
public Task(int timeout) {
this.timeout = timeout;
}
@Override
public void run() {
try {
//打印自定义线程名
log.info("当前线程名称:{}",Thread.currentThread().getName());
//使当前线程休眠指定时间
Thread.sleep(timeout * 1000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package com.kang.mongodb.pool;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
/**
* @Author Emperor Kang
* @ClassName MainTest
* @Description TODO
* @Date 2023/8/7 14:11
* @Version 1.0
* @Motto 让营地比你来时更干净
*/
public class MainTest {
public static void main(String[] args) {
// 自定义线程池名称
ThreadFactory threadFactory = new CustomizableThreadFactory("bigdata-thread-pool-");
// 创建带监控的线程池
MonitorThreadPool monitorThreadPool = new MonitorThreadPool(1, 3, 0, TimeUnit.SECONDS, new LinkedBlockingDeque<>(2),threadFactory);
try {
// 提交多个任务
for (int i = 10; i > 0; i--) {
// 创建任务
Task task = new Task(i);
// 提交任务
monitorThreadPool.submit(task);
// 每隔500毫秒提交一个
Thread.sleep(500);
}
// 使主线程休眠6秒钟
Thread.sleep(6000);
// 关闭线程池之前获取一次线程池情况
monitorThreadPool.monitor();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
// 关闭线程池
monitorThreadPool.shutdown();
}
}
}
执行程序观察输出
MonitorThreadPool - beforeExecute
MonitorThreadPool - 正在工作的线程数:1
MonitorThreadPool - 当前存在的线程数:1
MonitorThreadPool - 历史最大的线程数:1
MonitorThreadPool - 已提交的任务总数:1
MonitorThreadPool - 已完成的任务数:0
MonitorThreadPool - 队列中的任务数:0
MonitorThreadPool - ================线程池华丽分割线================
Task - 当前线程名称:bigdata-thread-pool-1
MonitorThreadPool - beforeExecute
MonitorThreadPool - 正在工作的线程数:2
MonitorThreadPool - 当前存在的线程数:2
MonitorThreadPool - 历史最大的线程数:2
MonitorThreadPool - 已提交的任务总数:4
MonitorThreadPool - 已完成的任务数:0
MonitorThreadPool - 队列中的任务数:2
MonitorThreadPool - ================线程池华丽分割线================
Task - 当前线程名称:bigdata-thread-pool-2
MonitorThreadPool - beforeExecute
MonitorThreadPool - 正在工作的线程数:3
MonitorThreadPool - 当前存在的线程数:3
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:0
MonitorThreadPool - 队列中的任务数:2
MonitorThreadPool - ================线程池华丽分割线================
14:28:24.815 [bigdata-thread-pool-3] INFO com.kang.mongodb.pool.Task - 当前线程名称:bigdata-thread-pool-3
Exception in thread "main" java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@14bf9759 rejected from com.kang.mongodb.pool.MonitorThreadPool@553f17c[Running, pool size = 3, active threads = 3, queued tasks = 2, completed tasks = 0]
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
at com.kang.mongodb.pool.MainTest.main(MainTest.java:29)
MonitorThreadPool - afterExecute
MonitorThreadPool - 正在工作的线程数:3
MonitorThreadPool - 当前存在的线程数:3
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:0
MonitorThreadPool - 队列中的任务数:2
MonitorThreadPool - ================线程池华丽分割线================
MonitorThreadPool - beforeExecute
MonitorThreadPool - 正在工作的线程数:3
MonitorThreadPool - 当前存在的线程数:3
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:1
MonitorThreadPool - 队列中的任务数:1
MonitorThreadPool - ================线程池华丽分割线================
Task - 当前线程名称:bigdata-thread-pool-3
MonitorThreadPool - afterExecute
MonitorThreadPool - 正在工作的线程数:3
MonitorThreadPool - 当前存在的线程数:3
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:1
MonitorThreadPool - 队列中的任务数:1
MonitorThreadPool - ================线程池华丽分割线================
MonitorThreadPool - beforeExecute
MonitorThreadPool - 正在工作的线程数:3
MonitorThreadPool - 当前存在的线程数:3
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:2
MonitorThreadPool - 队列中的任务数:0
MonitorThreadPool - ================线程池华丽分割线================
Task - 当前线程名称:bigdata-thread-pool-2
MonitorThreadPool - afterExecute
MonitorThreadPool - 正在工作的线程数:3
MonitorThreadPool - 当前存在的线程数:3
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:2
MonitorThreadPool - 队列中的任务数:0
MonitorThreadPool - ================线程池华丽分割线================
MonitorThreadPool - afterExecute
MonitorThreadPool - 正在工作的线程数:2
MonitorThreadPool - 当前存在的线程数:2
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:3
MonitorThreadPool - 队列中的任务数:0
MonitorThreadPool - ================线程池华丽分割线================
MonitorThreadPool - afterExecute
MonitorThreadPool - 正在工作的线程数:1
MonitorThreadPool - 当前存在的线程数:1
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:4
MonitorThreadPool - 队列中的任务数:0
MonitorThreadPool - ================线程池华丽分割线================
MonitorThreadPool - terminated
MonitorThreadPool - 正在工作的线程数:0
MonitorThreadPool - 当前存在的线程数:0
MonitorThreadPool - 历史最大的线程数:3
MonitorThreadPool - 已提交的任务总数:5
MonitorThreadPool - 已完成的任务数:5
MonitorThreadPool - 队列中的任务数:0
MonitorThreadPool - ================线程池华丽分割线================