Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1517476
  • 博文数量: 399
  • 博客积分: 8508
  • 博客等级: 中将
  • 技术积分: 5302
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-14 09:28
个人简介

能力强的人善于解决问题,有智慧的人善于绕过问题。 区别很微妙,小心谨慎做后者。

文章分类

全部博文(399)

文章存档

2018年(3)

2017年(1)

2016年(1)

2015年(69)

2013年(14)

2012年(17)

2011年(12)

2010年(189)

2009年(93)

分类: 架构设计与优化

2015-05-01 17:04:32

  public class MyExecutor extends ThreadPoolExecutor {

   public MyExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) {
       super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue);
       startTimes=new ConcurrentHashMap<>();
    }
            private ConcurrentHashMap startTimes;

             @Override
             protected void beforeExecute(Thread t, Runnable r) {

                 startTimes.put(String.valueOf(r.hashCode()), new Date());
             }

             @Override
             protected void afterExecute(Runnable r, Throwable t) {

                 Future result=(Future)r;
                 try {

                     Date startDate=startTimes.remove(String.valueOf(r.hashCode()));
                     Date finishDate=new Date();
                     long diff=finishDate.getTime()-startDate.getTime();
                     System.out.printf("MyExecutor: Duration: %d\n",diff);

                  } catch (InterruptedException | ExecutionException e) { e.printStackTrace();}
          }
}
阅读(4976) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~