JUC - Monitor监控ThreadPoolExecutor
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
参考阅读
- Java Thread Pool – ThreadPoolExecutor Example
- ThreadPoolExecutor – Java Thread Pool Example
- Java 多线程(5):Fork/Join 型线程池与 Work-Stealing 算法
JUC - Monitor监控ThreadPoolExecutor的更多相关文章
- 硬核干货:4W字从源码上分析JUC线程池ThreadPoolExecutor的实现原理
前提 很早之前就打算看一次JUC线程池ThreadPoolExecutor的源码实现,由于近段时间比较忙,一直没有时间整理出源码分析的文章.之前在分析扩展线程池实现可回调的Future时候曾经提到并发 ...
- 运用Real Spy Monitor监控网络
Real Spy Monitor是一个监测互联网和个人电脑,以保障其安全的软件.包括键盘敲击.网页站点.视窗开关.程序执行.屏幕扫描以及文件的出入等都是其监控的对象. 1.添加使用密码 在使用Real ...
- Jmeter性能测试之Monitor监控(SSHMon Samples Collector)
前面写的一篇Monitor监控有缺陷, 这篇文章使用Jmeter4.0+的版本, 使用插件SSHMon Samples Collector来做资源监控 1. 官网下载插件: plugins-manag ...
- Druid Monitor监控Java Web和Java SE项目
Druid Monitor 对于数据源,大家已经接触了不少了.比如c3p0.dhcp.proxool等,之后又发现使用tomcat-jdbc可以大大的提高性能.但是针对于我们的高并发的系统来说,总希望 ...
- Process Monitor监控进程操作注册表如何实现?
http://zhidao.baidu.com/link?url=Kqav4qkQSprC5FnpHPOGJvhqvY9fJ9-Vdx9g_SWh4w5VOusdRJo4Vl7qIdrG4LwRJvr ...
- Java - "JUC线程池" ThreadPoolExecutor原理解析
Java多线程系列--“JUC线程池”02之 线程池原理(一) ThreadPoolExecutor简介 ThreadPoolExecutor是线程池类.对于线程池,可以通俗的将它理解为"存 ...
- Python之Monitor监控线程(干货)
在日常工作中常遇到这样的情况,我们需要一个监控线程用于随时的获得其他进程的任务请求,或者我们需要监视某些资源等的变化,一个高效的Monitor程序如何使用python语言实现呢?为了解决上述问题,我将 ...
- redis-cli -h xxxxx -p xxxx monitor 监控host为xxxx,端口为xxx,redis连接及读写操作
# redis-cli -p monitor OK ] " lua] " lua] " "-1"
- [Monitor] 监控规则定义
系统监控规则:
随机推荐
- MySQL 性能调优
MySQL 性能调优 索引 索引是什么 官方介绍索引是帮助MySQL高效获取数据的数据结构.笔者理解索引相当于一本书的目录,通过目录就知道要的资料在哪里,不用一页一页查阅找出需要的资料. 索引目的 ...
- easyui datagrid的行编辑器editor 如何实现新增时可修改,编辑时,不可修改
项目出现一个需求,要求用户界面的用户名,新增时,可自由输入,编辑时,不可修改 html页面 <table id="gridlist" data-bind="data ...
- jconsole远程连接centos7 服务器上的tomcat来查看服务器状况(无密码版)
1.修改tomcat catalina.sh 代码解释:-Dcom.sun.management.jmxremote.port=6969 //连接端口,自定义不要与已有的端口冲突-Dcom.sun. ...
- Java基础 awt Frame 窗体在屏幕的中间显示
JDK :OpenJDK-11 OS :CentOS 7.6.1810 IDE :Eclipse 2019‑03 typesetting :Markdown code ...
- iobit-unlocker --- 类似 Unlocker 工具,强制删除文件或文件夹
win10 使用 Unlocker 1.9.2 常有问题,以前在win7上使用完全ok的 更换成:iobit-unlocker ,win10体验还可以,类似Unlocker 下载地址: https:/ ...
- 解决pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path问题
解决方案: 找到python的安装路径下的pytesseract: 例如我的是 C:\develop\Python\Lib\site-packages\pytesseract .用文本编辑器打开 ...
- mark_Linux_wc
Linux wc命令 Linux wc命令用于计算字数. 利用wc指令我们可以计算文件的Byte数.字数.或是列数,若不指定文件名称.或是所给予的文件名为"-",则wc指令会从标准 ...
- SpringBoot处理异常方式
SpringBoot提供了多种处理异常方式,以下为常用的几种 1. 自定义错误异常页面 SpringBoot默认的处理异常的机制:SpringBoot默认的已经提供了一套处理异常的机制.一旦程序中出现 ...
- Python - Django - 中间件 process_template_response
process_template_response(self, request, response) 有两个参数,response 是 TemplateResponse 对象(由视图函数或者中间件产生 ...
- nginx过滤access_log中HEAD、OPTIONS请求记录
网上很多教程说是这样做: if ($request_method = HEAD) { access_log off; } 试了之后是不行的,正确的做法如下: http { map $request_m ...