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了五个线程,然后分别对这五个线程进行统计,最后统计出各个线程的耗时平均时间。

最新文章

  1. PHP 装饰器模式
  2. (原)android中的动画(二)
  3. ios-改变button四个角的弧度
  4. BZOJ4298 : [ONTAK2015]Bajtocja
  5. Linux一些常用软件的源码安装
  6. Yii render和renderPartial的区别
  7. o​r​a​l​c​e​ ​D​B​A​ ​培​训_lesson06
  8. 特殊字符 js处理
  9. Git多帐号配置,管理多个SSH
  10. IntelliJ IDEA的编译设置
  11. ajax基本原理与案例
  12. [android]android下apk的安装过程
  13. onchange 事件
  14. Codecraft-18 and Codeforces Round #458 (Div. 1 + Div. 2, combined)G. Sum the Fibonacci
  15. php loop循环 拿到键名
  16. PHP实现微信发红包功能2
  17. oracle 查看临时表空间temp 的使用情况以及扩展表空间
  18. Mac上zip,rar,tar文件命令解压和压缩
  19. POJ 1182 并查集
  20. 试着用React写项目-利用react-router解决跳转路由等问题(三)

热门文章

  1. UITextView文字上方一段空白的解决方法
  2. L149
  3. recovery.conf文件详解
  4. 动态PIVOT行转列
  5. python pass关键字神奇吗
  6. iot_programe Makefile hacking
  7. 【集成学习】安装lightgbm
  8. MySQL 实用技巧
  9. vs2010 创建和C#使用动态链接库(dll)
  10. iis 部署 未在本地计算机上注册“Microsoft.Jet.OleDb.4.0”提供程序