JUC - Monitor监控ThreadPoolExecutor

一个自定义Monitor监控ThreadPoolExecutor的执行情况

TASK

WokerTask

class WorkerTask implements Runnable{
private String command; public WorkerTask(String command) {
this.command = command;
} @Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
processCommand();
System.out.println(Thread.currentThread().getName()+" End.");
} private void processCommand(){
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
} } @Override
public String toString() {
return "WorkerTask{" +
"command='" + command + '\'' +
'}';
}
}

MonitorTask(监听器)

class MonitorTask implements Runnable{
// 被监控的executor
private final ThreadPoolExecutor executor;
// 监控间隔
private final int seconds;
// 监控开关
private boolean run = true; public MonitorTask(ThreadPoolExecutor executor, int seconds) {
this.executor = executor;
this.seconds = seconds;
} public void shutdown(){
this.run = false;
}
@Override
public void run() {
while (run) {
System.out.println(
String.format("%s - [monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, Queue: %d, isShutdown: %s, isTerminated: %s",
Thread.currentThread().getName(),
this.executor.getPoolSize(),
this.executor.getCorePoolSize(),
this.executor.getActiveCount(),
this.executor.getCompletedTaskCount(),
this.executor.getTaskCount(),
this.executor.getQueue().size(),
this.executor.isShutdown(),
this.executor.isTerminated()));
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}

RejectedExecutionHandler(拒绝策略)

LogRejectedExecutionHandler

    class LogRejectedExecutionHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// 当任务队列满了,并且达到maximumPoolSize时的拒绝策略
System.out.println(r.toString() + " is rejected");
}
}

ThreadPoolExecutor

ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
2,
4,
10, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(2),
Executors.defaultThreadFactory(),
new LogRejectedExecutionHandler()
);

完整的例子

public class ThreadPoolExecutorMonitorSimple {
public static void main(String[] args) throws InterruptedException {
final ThreadPoolExecutor poolExecutor = new ThreadPoolExecutor(
2,
4,
10, TimeUnit.SECONDS,
new ArrayBlockingQueue<>(2),
Executors.defaultThreadFactory(),
new LogRejectedExecutionHandler()
); // 创建监控任务,间隔3s
final MonitorTask monitorTask = new MonitorTask(poolExecutor, 3); // 监听任务启动
new Thread(monitorTask).start(); for (int i = 0; i < 10; i++) {
poolExecutor.execute(new WorkerTask("cmd-"+i));
} TimeUnit.SECONDS.sleep(60); poolExecutor.shutdown(); TimeUnit.SECONDS.sleep(5); monitorTask.shutdown();
} static class LogRejectedExecutionHandler implements RejectedExecutionHandler {
@Override
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
// 当任务队列满了,并且达到maximumPoolSize时的拒绝策略
System.out.println(r.toString() + " is rejected");
}
} static class WorkerTask implements Runnable{
private String command; public WorkerTask(String command) {
this.command = command;
} @Override
public void run() {
System.out.println(Thread.currentThread().getName()+" Start. Command = "+command);
processCommand();
System.out.println(Thread.currentThread().getName()+" End.");
} private void processCommand(){
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
} } @Override
public String toString() {
return "WorkerTask{" +
"command='" + command + '\'' +
'}';
}
} static class MonitorTask implements Runnable{
// 被监控的executor
private final ThreadPoolExecutor executor;
// 监控间隔
private final int seconds;
// 监控开关
private boolean run = true; public MonitorTask(ThreadPoolExecutor executor, int seconds) {
this.executor = executor;
this.seconds = seconds;
} public void shutdown(){
this.run = false;
}
@Override
public void run() {
while (run) {
System.out.println(
String.format("%s - [monitor] [%d/%d] Active: %d, Completed: %d, Task: %d, Queue: %d, isShutdown: %s, isTerminated: %s",
Thread.currentThread().getName(),
this.executor.getPoolSize(),
this.executor.getCorePoolSize(),
this.executor.getActiveCount(),
this.executor.getCompletedTaskCount(),
this.executor.getTaskCount(),
this.executor.getQueue().size(),
this.executor.isShutdown(),
this.executor.isTerminated()));
try {
TimeUnit.SECONDS.sleep(seconds);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
}

日志:

pool-1-thread-1 Start. Command = cmd-0
pool-1-thread-2 Start. Command = cmd-1
pool-1-thread-3 Start. Command = cmd-4
WorkerTask{command='cmd-6'} is rejected
WorkerTask{command='cmd-7'} is rejected
WorkerTask{command='cmd-8'} is rejected
WorkerTask{command='cmd-9'} is rejected
pool-1-thread-4 Start. Command = cmd-5
Thread-0 - [monitor] [0/2] Active: 0, Completed: 0, Task: 1, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [4/2] Active: 4, Completed: 0, Task: 6, Queue: 2, isShutdown: false, isTerminated: false
pool-1-thread-1 End.
pool-1-thread-1 Start. Command = cmd-2
pool-1-thread-2 End.
pool-1-thread-2 Start. Command = cmd-3
pool-1-thread-3 End.
pool-1-thread-4 End.
Thread-0 - [monitor] [4/2] Active: 2, Completed: 4, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [4/2] Active: 2, Completed: 4, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
pool-1-thread-1 End.
pool-1-thread-2 End.
Thread-0 - [monitor] [4/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [2/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: false, isTerminated: false
Thread-0 - [monitor] [0/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: true, isTerminated: true
Thread-0 - [monitor] [0/2] Active: 0, Completed: 6, Task: 6, Queue: 0, isShutdown: true, isTerminated: true

参考阅读

JUC - Monitor监控ThreadPoolExecutor的更多相关文章

  1. 硬核干货:4W字从源码上分析JUC线程池ThreadPoolExecutor的实现原理

    前提 很早之前就打算看一次JUC线程池ThreadPoolExecutor的源码实现,由于近段时间比较忙,一直没有时间整理出源码分析的文章.之前在分析扩展线程池实现可回调的Future时候曾经提到并发 ...

  2. 运用Real Spy Monitor监控网络

    Real Spy Monitor是一个监测互联网和个人电脑,以保障其安全的软件.包括键盘敲击.网页站点.视窗开关.程序执行.屏幕扫描以及文件的出入等都是其监控的对象. 1.添加使用密码 在使用Real ...

  3. Jmeter性能测试之Monitor监控(SSHMon Samples Collector)

    前面写的一篇Monitor监控有缺陷, 这篇文章使用Jmeter4.0+的版本, 使用插件SSHMon Samples Collector来做资源监控 1. 官网下载插件: plugins-manag ...

  4. Druid Monitor监控Java Web和Java SE项目

    Druid Monitor 对于数据源,大家已经接触了不少了.比如c3p0.dhcp.proxool等,之后又发现使用tomcat-jdbc可以大大的提高性能.但是针对于我们的高并发的系统来说,总希望 ...

  5. Process Monitor监控进程操作注册表如何实现?

    http://zhidao.baidu.com/link?url=Kqav4qkQSprC5FnpHPOGJvhqvY9fJ9-Vdx9g_SWh4w5VOusdRJo4Vl7qIdrG4LwRJvr ...

  6. Java - "JUC线程池" ThreadPoolExecutor原理解析

    Java多线程系列--“JUC线程池”02之 线程池原理(一) ThreadPoolExecutor简介 ThreadPoolExecutor是线程池类.对于线程池,可以通俗的将它理解为"存 ...

  7. Python之Monitor监控线程(干货)

    在日常工作中常遇到这样的情况,我们需要一个监控线程用于随时的获得其他进程的任务请求,或者我们需要监视某些资源等的变化,一个高效的Monitor程序如何使用python语言实现呢?为了解决上述问题,我将 ...

  8. redis-cli -h xxxxx -p xxxx monitor 监控host为xxxx,端口为xxx,redis连接及读写操作

    # redis-cli -p monitor OK ] " lua] " lua] " "-1"

  9. [Monitor] 监控规则定义

    系统监控规则:

随机推荐

  1. Struts2工作原理和核心文件

    一.Struts2工作原理 如下图: 二.Struts2配置文件 1.web.xml 任何MVC框架都需要与Web应用整合,这就不得不借助于web.xml文件,只有配置了web.xml文件的Servl ...

  2. 【用例篇】Xmind转为csv 导入禅道

    用过禅道的都知道,用例维护实在是太不方便了,有人推荐了一种方法,用Xmind先编写用例(思路比较清晰),写好之后借助工具(xmind2testcase)将用例转为CSV格式,之后再导入禅道 参考资料: ...

  3. dashi 成长 > 领导 > 平台 > 钱 人品 态度 能力 价值

    https://promotion.aliyun.com/ntms/yunparter/invite.html?userCode=thy3557s https://www.aliyun.com/min ...

  4. tp使用ajaxReturn返回二维数组格式的字符串,前台如何获取非乱码

    参考: https://www.cnblogs.com/jiqing9006/p/5000849.html https://blog.csdn.net/zengxiangxuan123456/arti ...

  5. (转)Nginx中sendfile的作用

    原文:https://blog.csdn.net/zhusixun/article/details/81702380 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上 ...

  6. Go 汇编入门

    https://golang.org/doc/asm https://github.com/teh-cmc/go-internals/tree/master/chapter1_assembly_pri ...

  7. QWidget添加带有图片的QPushButton,布局QGridLayout

    QWidget* w = new QWidget(this); w->setGeometry(10,20,400,300); QVBoxLayout* layout = new QVBoxLay ...

  8. VLOOKUP使用方法

    VLOOKUP函数是常用的一个内容查找函数,用于通过某一条件查询数据源中需要的内容.语法:=VLOOKUP(查询值,数据源,显示序列,匹配参数)1)查询值:匹配的key值2)数据源:查找范围,1)起点 ...

  9. .NET 文件上传和文件接收

    有时候,我们需要在后台端发起向指定的“文件接收接口”的文件传输请求,可以采用HttpWebRequest方式实现文件传输请求. 1.HttpWebRequest文件传输请求的代码如下: 其中,url为 ...

  10. Spring Cloud 微服务技术整合

    微服务架构风格是一种使用一套小服务来开发单个应用的方式途径,每个服务运行在自己的进程中,并使用轻量级机制通信,通常是HTTP API,这些服务基于业务能力构建,并能够通过自动化部署机制来独立部署,这些 ...