ThreadPoolExecutor之三:自定义线程池-扩展示例
ThreadPoolExecutor是可扩展的,下面一个示例:
package com.dxz.threadpool.demo1; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong; public class StatThreadPoolExecutor extends ThreadPoolExecutor { private final ThreadLocal<Long> startTime = new ThreadLocal<Long>();
private final AtomicLong numTasks = new AtomicLong();
private final AtomicLong totalTime = new AtomicLong(); public StatThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit,
BlockingQueue<Runnable> workQueue) {
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
} @Override
protected void beforeExecute(Thread t, Runnable r) {
super.beforeExecute(t, r);
System.out.println(String.format("beforeExecute() Thread '%s': start '%s'", t, r));
startTime.set(System.nanoTime());
} @Override
protected void afterExecute(Runnable r, Throwable t) {
try {
long endTime = System.nanoTime();
long taskTime = endTime - startTime.get();
numTasks.incrementAndGet();
totalTime.addAndGet(taskTime);
System.out.println(String.format("afterExecute() : end '%s', time=%dns", r, taskTime));
} finally {
super.afterExecute(r, t);
}
} @Override
protected void terminated() {
try {
System.out.println(String.format("terminated() Terminated: avg time=%dns", totalTime.get() / numTasks.get()));
} finally {
super.terminated();
}
}
}
启动程序:
package com.dxz.threadpool.demo1; import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit; public class StatClient {
public static void main(String[] args) {
ThreadPoolExecutor exec = new StatThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
exec.execute(new Thread(new Printer(5),"t5"));
exec.execute(new Thread(new Printer(4),"t4"));
exec.execute(new Thread(new Printer(3),"t3"));
exec.execute(new Thread(new Printer(2),"t2"));
exec.execute(new Thread(new Printer(1),"t1"));
exec.shutdown();
} } class Printer implements Runnable {
private int sleepTime; public Printer(int sleepTime) {
this.sleepTime = sleepTime;
} @Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is running.");
try {
TimeUnit.SECONDS.sleep(sleepTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
} }
结果:
beforeExecute() Thread 'Thread[pool-1-thread-5,5,main]': start 'Thread[t1,5,main]'
beforeExecute() Thread 'Thread[pool-1-thread-4,5,main]': start 'Thread[t2,5,main]'
beforeExecute() Thread 'Thread[pool-1-thread-1,5,main]': start 'Thread[t5,5,main]'
beforeExecute() Thread 'Thread[pool-1-thread-2,5,main]': start 'Thread[t4,5,main]'
beforeExecute() Thread 'Thread[pool-1-thread-3,5,main]': start 'Thread[t3,5,main]'
pool-1-thread-2 is running.
pool-1-thread-1 is running.
pool-1-thread-5 is running.
pool-1-thread-4 is running.
pool-1-thread-3 is running.
afterExecute() : end 'Thread[t1,5,main]', time=1001000273ns
afterExecute() : end 'Thread[t2,5,main]', time=2001157367ns
afterExecute() : end 'Thread[t3,5,main]', time=3000630301ns
afterExecute() : end 'Thread[t4,5,main]', time=4000804066ns
afterExecute() : end 'Thread[t5,5,main]', time=5001279195ns
terminated() Terminated: avg time=3000974240ns
可以看到,在测试类client中通过execute了五个线程,然后分别对这五个线程进行统计,最后统计出各个线程的耗时平均时间。
ThreadPoolExecutor之三:自定义线程池-扩展示例的更多相关文章
- 基于ThreadPoolExecutor,自定义线程池简单实现
一.线程池作用 在上一篇随笔中有提到多线程具有同一时刻处理多个任务的特点,即并行工作,因此多线程的用途非常广泛,特别在性能优化上显得尤为重要.然而,多线程处理消耗的时间包括创建线程时间T1.工作时间T ...
- Android线程管理之ThreadPoolExecutor自定义线程池
前言: 上篇主要介绍了使用线程池的好处以及ExecutorService接口,然后学习了通过Executors工厂类生成满足不同需求的简单线程池,但是有时候我们需要相对复杂的线程池的时候就需要我们自己 ...
- 自定义线程池的名称(ThreadPoolExecutor)
目的:有时候为了快速定位出现错误的位置,在采用线程池时我们需要自定义线程池的名称. 1.创建ThreadFactory(ThreadPoolExecutor默认采用的是DefaultThreadFac ...
- 自定义线程池ThreadPoolExecutor
使用自定义的方式创建线程池 Java本身提供的获取线程池的方式 使用Executors直接获取线程池,注意,前四个方式的底层都是通过new ThreadPoolExecutor()的方式创建的线程池, ...
- java多线程(四)-自定义线程池
当我们使用 线程池的时候,可以使用 newCachedThreadPool()或者 newFixedThreadPool(int)等方法,其实我们深入到这些方法里面,就可以看到它们的是实现方式是这样的 ...
- Java自定义线程池-记录每个线程执行耗时
ThreadPoolExecutor是可扩展的,其提供了几个可在子类化中改写的方法,如下: protected void beforeExecute(Thread t, Runnable r) { } ...
- SpringBoot 自定义线程池
本教程目录: 自定义线程池 配置spring默认的线程池 1. 自定义线程池 1.1 修改application.properties task.pool.corePoolSize=20 task.p ...
- Executors提供的四种线程池和自定义线程池
JAVA并发编程——EXECUTORS 线程池的思想是一种对象池的思想,开放一块内存空间,里面存放了众多(未死亡)的线程,池中线程执行调度由池管理器来处理.当有线程任务时,从池中取一个,执行完毕,对象 ...
- Android AsyncTask 深度理解、简单封装、任务队列分析、自定义线程池
前言:由于最近在做SDK的功能,需要设计线程池.看了很多资料不知道从何开始着手,突然发现了AsyncTask有对线程池的封装,so,就拿它开刀,本文将从AsyncTask的基本用法,到简单的封装,再到 ...
随机推荐
- 20165202 预备作业3 Linux安装及学习
一.虚拟机安装 娄老师的<基于VirtualBox安装Ubuntu图文教程>对于安装过程的介绍很易懂,但在安装过程中还是遇到了一些问题 Q1:安装教程中下载地址的VM提示安装包损坏 解决办 ...
- New Concept English there (7)
27w/m Has it ever happened to you? Have you ever put your trousers in the washing machine and then r ...
- Android深入理解JNI(二)类型转换、方法签名和JNIEnv
相关文章 Android深入理解JNI系列 前言 上一篇文章介绍了JNI的基本原理和注册,这一篇接着带领大家来学习JNI的数据类型转换.方法签名和JNIEnv. 1.数据类型的转换 首先给出上一篇文章 ...
- KAFKA 0.11 RHEL6.5安装
KAFKA简介 KAFKA是一款分布式消息发布和订阅的系统. 官网:http://kafka.apache.org/ 1.下载KAFKA及JDK KAFKA下载地址: http://kafka.apa ...
- JavaScript库基本格式写法
/********************************************************************* * JavaScript库基本格式写法 * 说明: * 由 ...
- windows中的oracle12SE后启动的系统服务的列表
下图是我安装在windows 10下安装好oracle12.10SE之后的启动的系统服务的列表. 通常,我是将其全部修改为手动启动.当需要用oracle服务的时候,只需要启动对应的实例的服务和tnsl ...
- 推荐sinaapp谷歌搜索引擎,firefox自定义搜索引擎
满园春色关不住,从此不用再FQ. 一直都在用谷歌,也一直都被和谐. 直到在迅影实习,知道了这个网站:http://goog.sinaapp.com/ 下面说说在firefox里面添加搜索引擎 首先打开 ...
- Django项目部署(阿里云)(2)--扩展
新博客地址:http://muker.net/django-server-two.html 前面的只是最简单的部署,真实情况总是更复杂一点实际流程大概是这么操作的(我这种菜鸟的想法):本地写代码-&g ...
- 颜色叠加模式:mix-blend-mode
文章转自叠加模式 http://www.cgspread.com/3551.html 注释:1.混合模式的数学计算公式,另外还介绍了不透明度.2.这些公式仅适用于RGB图像,对于Lab颜色图像而言,这 ...
- 复选框checkbox样式修改
该方法只兼容IE9及以上 将checkbox和label关联起来, 将checkbox隐藏掉,通过点击label来点击checkbox,label的样式即可自定义. 通过checkbox:checke ...