J.U.C--扩展ThreadPoolExecutor
来源:互联网 发布:淘宝tab是什么意思 编辑:程序博客网 时间:2024/06/05 17:14
ThreadPoolExecutor是可以扩展的,它提供了几个在子类中改写的方法,比如:beforeExecute()、afterExecute()、和terminated()方法。这些方法可以扩展ThreadPoolExecutor的执行行为。比如剪监控线程池或则是添加日志之类的。
下面是一个例子,给线程池添加统计信息:
package thread;import org.apache.log4j.Logger;import java.util.concurrent.*;import java.util.concurrent.atomic.AtomicLong;/** * Created by louyuting on 17/1/12. */public class TimingThreadPool extends ThreadPoolExecutor{ public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue); } public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); } public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory); } public TimingThreadPool(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); } private final ThreadLocal<Long> startTime = new ThreadLocal<Long>(); private final Logger log = Logger.getLogger("TimingThreadPool"); private final AtomicLong numTasks = new AtomicLong(); private final AtomicLong totalTime = new AtomicLong(); @Override protected void beforeExecute(Thread t, Runnable r) { super.beforeExecute(t, r); System.out.println(String.format("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("Thread %s end %s time=%dns", t,r,taskTime)); } finally { super.afterExecute(r,t); } } @Override protected void terminated() { super.terminated(); } public static void main(String[] args) { TimingThreadPool t = new TimingThreadPool(1,1,0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); for(int i=0; i<3; i++){ t.submit(new Runnable() { @Override public void run() { System.out.println("test"); } }); } t.shutdown(); }}
运行结果如下
0 0
- J.U.C--扩展ThreadPoolExecutor
- J.U.C--线程池ThreadPoolExecutor
- J.U.C--ThreadPoolExecutor的配置与使用
- 【死磕Java并发】-----J.U.C之线程池:ThreadPoolExecutor
- J.U.C体系
- J.U.C
- “J.U.C”:ReentrantReadWriteLock
- “J.U.C”:Condition
- J.U.C重入锁
- J.U.C包介绍
- J.U.C包介绍
- J.U.C之Future
- J.U.C之CopyOnWriteArrayList
- J.U.C之CountDownLatch
- J.U.C之CyclicBarrier
- J.U.C之Semaphore
- J.U.C之Exchanger
- J.U.C之Lock
- 转载 :Android——编译安装Module的控制因素
- ios 约束冲突NSAutoresizingMaskLayoutConstraint
- toString() 和 强制类型转换 (String)
- pycharm 激活码
- 使用localhost:4000访问本地blog一直无响应
- J.U.C--扩展ThreadPoolExecutor
- 这里有一份面筋请查收(七)
- Oracle sql特殊字符处理
- 例题 10-12 纸牌游戏 UVa 1637
- (3)Kurento之系统搭建I-KMS6.0
- 使用阿里云的ip地址查询服务-使用java调用ip地址查询服务
- prototype和开房:你会用才能理解它
- [iOS]设置页面
- 2017-01-12 早 策略模式