博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
《 Java并发编程从入门到精通》 Java线程池的监控
阅读量:6907 次
发布时间:2019-06-27

本文共 2874 字,大约阅读时间需要 9 分钟。

本文是《 Java并发编程从入门到精通》第9章 线程的监控及其日常工作中如何分析的9.1节 Java线程池的监控。

 

看不到不等于不存在!让我们来看看工作中是如何找问题解决问题的。

鸟欲高飞先振翅,人求上进先读书。

京东,亚马逊,当当均有销售。


9.1 Java线程池的监控

如果想实现线程池的监控,必须要自定义线程池继承ThreadPoolExecutor类,并且实现beforeExecute,afterExecute和terminated方法,我们可以在任务执行前,执行后和线程池关闭前干一些事情。如监控任务的平均执行时间,最大执行时间和最小执行时间等。这几个方法在线程池里是空方法。如:

//每执行一个工作任务线程之前都会执行此实现的方法

protected void beforeExecute(Thread t, Runnable r) {

//t – 放在线程池里面要执行的线程。

//r – 将要执行这个线程的线程池里面的工作线程。

}

 

//每执行一个工作任务线程之后都会执行的方法

protected void afterExecute(Runnable r, Throwable t) {

//r – 已经运行结束的工作线程。

//t – 运行异常。

}

 

//线程池关闭之前可以干一些事情;

protected void terminated() { };

 

线程池里有一些属性在监控线程池的时候可以使用: 

  1. taskCount:线程池需要执行的任务数量。
  2. completedTaskCount:线程池在运行过程中已完成的任务数量。小于或等于taskCount。
  3. largestPoolSize:线程池曾经创建过的最大线程数量。通过这个数据可以知道线程池是否满过。如等于线程池的最大大小,则表示线程池曾经满了。
  4. getPoolSize:线程池的线程数量。如果线程池不销毁的话,池里的线程不会自动销毁,所以这个大小只增不+
  5. getActiveCount:获取活动的线程数。

 

大家想一想如果你来写的话如何去写,提供实例demo如下,慢慢体会一下:

public class MonitorThreadPoolExecutorDemo {

public static void main(String[] args) throws InterruptedException, ExecutionException {

Thread. sleep(500L);// 方便测试

ExecutorService executor = new MonitorThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS , new LinkedBlockingQueue() );

for (int i = 0; i < 3; i++) {

Runnable runnable = new Runnable() {

public void run() {

try {

Thread. sleep(100L);

catch (InterruptedException e) {

e.printStackTrace();

}

}

};

executor.execute(runnable);

}

executor.shutdown();

System. out.println(“Thread Main End!” );

}

}

class MonitorThreadPoolExecutor extends ThreadPoolExecutor {

public MonitorThreadPoolExecutor(int arg0, int arg1, long arg2, TimeUnit arg3, BlockingQueue<Runnable> arg4) {

super(arg0, arg1, arg2, arg3, arg4);

}

protected void beforeExecute(Thread paramThread, Runnable paramRunnable) {

System. out.println(“work_task before:” + paramThread.getName());

}

protected void afterExecute(Runnable r, Throwable t) {

super.afterExecute(r, t);

System. out.println(“work_task after worker thread is :” + r);

}

protected void terminated() {

System. out.println(“terminated getCorePoolSize:” + this.getCorePoolSize() + “;getPoolSize:” + this.getPoolSize() + “;getTaskCount:” + this .getTaskCount() + “;getCompletedTaskCount:”

this.getCompletedTaskCount() + “;getLargestPoolSize:” + this.getLargestPoolSize() + “;getActiveCount:” + this.getActiveCount());

System. out.println(“ThreadPoolExecutor terminated:” );

}

}

运行结果如下:

work_task before:pool-1-thread-1

work_task before:pool-1-thread-3

work_task before:pool-1-thread-2

Thread Main End!

work_task after worker thread is :demo.thread.MonitorThreadPoolExecutorDemo$1@13bbe97

work_task after worker thread is :demo.thread.MonitorThreadPoolExecutorDemo$1@70125b

work_task after worker thread is :demo.thread.MonitorThreadPoolExecutorDemo$1@ceba90

terminated getCorePoolSize:5;getPoolSize:0;getTaskCount:3;getCompletedTaskCount:3;getLargestPoolSize:3;getActiveCount:0

ThreadPoolExecutor terminated:

 

  • 转载自 
你可能感兴趣的文章
实例化需求的优点
查看>>
Linux管理常见错误的解决方法
查看>>
MySQL架构优化实战系列3:定时计划任务与表分区
查看>>
kafka - advertised.listeners and listeners
查看>>
Hadoop YARN学习监控JVM和实时监控Ganglia、Ambari(5)
查看>>
ECharts:免费,开源,超炫的可视化作品
查看>>
跨界 +赋能——互联网的下一个关键词
查看>>
argz_create函数
查看>>
vmware HA与vmware FT功能对比
查看>>
分区表添加分区的问题
查看>>
从数据库生成和控制treeview
查看>>
linux基础:vbox+ubuntu环境,常见命令+基本脚本编写与执行
查看>>
面向物联网的几大开源操作系统
查看>>
百度分享按钮代码
查看>>
openCV vs2013配置
查看>>
Resin优化方案
查看>>
GC参数整理
查看>>
前后端常见的几种鉴权方式
查看>>
Oracle11g DMP 文件导入到 10g
查看>>
双网卡同时使用配置
查看>>