http://victorzhzh.iteye.com/blog/1011635

上一篇中我们看到了Timer的不足之处,本篇我们将围绕这些不足之处看看ScheduledThreadPoolExecutor是如何优化的。

为了研究方便我们需要两个类:

  1. public class Task1 implements Callable<String> {
  2. @Override
  3. public String call() throws Exception {
  4. String base = "abcdefghijklmnopqrstuvwxyz0123456789";
  5. Random random = new Random();
  6. StringBuffer sb = new StringBuffer();
  7. for (int i = 0; i < 10; i++) {
  8. int number = random.nextInt(base.length());
  9. sb.append(base.charAt(number));
  10. }
  11. System.out.println("Task1 running: " + new Date());
  12. return sb.toString();
  13. }
  14. }

生成含有10个字符的字符串,使用Callable接口目的是我们不再任务中直接输出结果,而主动取获取任务的结果

  1. public class LongTask implements Callable<String> {
  2. @Override
  3. public String call() throws Exception {
  4. System.out.println("LongTask running: "+new Date());
  5. TimeUnit.SECONDS.sleep(10);
  6. return "success";
  7. }
  8. }

长任务类,这里我们让任务沉睡10秒然后返回一个“success”

下面我们来分析一下ScheduledThreadPoolExecutor:

1、Timer中单线程问题是否在ScheduledThreadPoolExecutor中存在?

我们先来看一下面的程序:

  1. public class ScheduledThreadPoolExec {
  2. public static void main(String[] args) throws InterruptedException,
  3. ExecutionException {
  4. <strong><span style="color: #ff0000;">ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
  5. 2);</span>
  6. </strong>
  7. ScheduledFuture future1 = executor.schedule(new Task1(), 5,
  8. TimeUnit.SECONDS);
  9. ScheduledFuture future2 = executor.schedule(new LongTask(), 3,
  10. TimeUnit.SECONDS);
  11. BlockingQueue<ScheduledFuture> blockingQueue = new ArrayBlockingQueue<ScheduledFuture>(
  12. 2, true);
  13. blockingQueue.add(future2);
  14. blockingQueue.add(future1);
  15. System.out.println(new Date());
  16. while (!blockingQueue.isEmpty()) {
  17. ScheduledFuture future = blockingQueue.poll();
  18. if (!future.isDone())
  19. blockingQueue.add(future);
  20. else
  21. System.out.println(future.get());
  22. }
  23. System.out.println(new Date());
  24. executor.shutdown();
  25. }
  26. }

首先,我们定义了一个ScheduledThreadPoolExecutor它的池长度是2。接着提交了两个任务:第一个任务将延迟5秒执行,第二个任务将延迟3秒执行。我们建立了一个BlockingQueue,用它来存储了ScheduledFuture,使用ScheduledFuture可以获得任务的执行结果。在一个while循环中,我们每次将一个ScheduledFuture从队列中弹出,验证它是否被执行,如果没有被执行则再次将它加入队列中,如果被执行了,这使用ScheduledFuture的get方法获取任务执行的结果。看一下执行结果:

  1. Thu Apr 21 19:23:02 CST 2011
  2. LongTask running: Thu Apr 21 19:23:05 CST 2011
  3. Task1 running: Thu Apr 21 19:23:07 CST 2011
  4. h1o2wd942e
  5. success
  6. Thu Apr 21 19:23:15 CST 2011

我们看到长任务先运行,因为长任务只等待了3秒,然后是输出字符串的任务运行,两个任务开始时间相差2秒,而先输出了字符串而后才是长任务运行的结果success,最后我们查看一下整体的开始和结束时间相差了13秒。

这说明在ScheduledThreadPoolExecutor中它不是以一个线程运行任务的,而是以多个线程,如果用一个线程运行任务,那么长任务运行完之前是不会运行输出字符串任务的。其实这个“多个任务“是我们自己指定的注意一下标红的代码,如果我们把这行代码改为:

  1. ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);

那么运行结果就会发生变化,

  1. Thu Apr 21 19:36:56 CST 2011
  2. LongTask running: Thu Apr 21 19:36:59 CST 2011
  3. success
  4. Task1 running: Thu Apr 21 19:37:09 CST 2011
  5. y981iqd0or
  6. Thu Apr 21 19:37:09 CST 2011

这时其实和使用Timer运行结果是一样的。任务是在一个线程里顺序执行的。

2、Timer中一但有运行时异常报出后续任务是否还会正常运行?

为了研究这个问题,我们还是需要一个能够抛出异常的任务,如下:

  1. public class TimerExceptionTask extends TimerTask {
  2. @Override
  3. public void run() {
  4. System.out.println("TimerExceptionTask: "+new Date());
  5. throw new RuntimeException();
  6. }
  7. }

我们对上面运行任务的代码做一点点小小的修改,先运行两个抛出异常的任务,如下:

  1. public class ScheduledThreadPoolExec {
  2. public static void main(String[] args) throws InterruptedException,
  3. ExecutionException {
  4. ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
  5. 2);
  6. ScheduledFuture future1 = executor.schedule(new Task1(), 5,
  7. TimeUnit.SECONDS);
  8. ScheduledFuture future2 = executor.schedule(new LongTask(), 3,
  9. TimeUnit.SECONDS);
  10. <strong><span style="color: #ff0000;">executor.schedule(new TimerExceptionTask(), 1, TimeUnit.SECONDS);
  11. executor.schedule(new TimerExceptionTask(), 2, TimeUnit.SECONDS);</span>
  12. </strong>
  13. BlockingQueue<ScheduledFuture> blockingQueue = new ArrayBlockingQueue<ScheduledFuture>(
  14. 2, true);
  15. blockingQueue.add(future2);
  16. blockingQueue.add(future1);
  17. System.out.println(new Date());
  18. while (!blockingQueue.isEmpty()) {
  19. ScheduledFuture future = blockingQueue.poll();
  20. if (!future.isDone())
  21. blockingQueue.add(future);
  22. else
  23. System.out.println(future.get());
  24. }
  25. System.out.println(new Date());
  26. executor.shutdown();
  27. }
  28. }

注意,标红的代码,如果这两个代码抛出错误后会影响后续任务,那么就应该在此终止,但是看一下结果,

  1. Thu Apr 21 19:40:15 CST 2011
  2. TimerExceptionTask: Thu Apr 21 19:40:16 CST 2011
  3. TimerExceptionTask: Thu Apr 21 19:40:17 CST 2011
  4. LongTask running: Thu Apr 21 19:40:18 CST 2011
  5. Task1 running: Thu Apr 21 19:40:20 CST 2011
  6. v5gcf01iiz
  7. success
  8. Thu Apr 21 19:40:28 CST 2011

后续任务仍然执行,可能会有朋友说:“你上面的池设置的是2,所以很有可能是那两个抛出异常的任务都在同一个线程中执行,而另一个线程执行了后续的任务”。那我们就把ScheduledThreadPoolExecutor设置成1看看,结果如下:

  1. Thu Apr 21 19:43:00 CST 2011
  2. TimerExceptionTask: Thu Apr 21 19:43:01 CST 2011
  3. TimerExceptionTask: Thu Apr 21 19:43:02 CST 2011
  4. LongTask running: Thu Apr 21 19:43:03 CST 2011
  5. success
  6. Task1 running: Thu Apr 21 19:43:13 CST 2011
  7. 33kgv8onnd
  8. Thu Apr 21 19:43:13 CST 2011

后续任务也执行了,所以说ScheduledThreadPoolExecutor不会像Timer那样有线程泄漏现象。

对于周期性执行和Timer很类似这里就不再举例了。

定时且周期性的任务研究II--ScheduledThreadPoolExecutor的更多相关文章

  1. 定时且周期性的任务研究I--Timer

    很多时候我们希望任务可以定时的周期性的执行,在最初的JAVA工具类库中,通过Timer可以实现定时的周期性的需求,但是有一定的缺陷,例如:Timer是基于绝对时间的而非支持相对时间,因此Timer对系 ...

  2. (CSDN 迁移) JAVA多线程实现-支持定时与周期性任务的线程池(newScheduledThreadPool)

    前几篇文章中分别介绍了 单线程化线程池(newSingleThreadExecutor) 可控最大并发数线程池(newFixedThreadPool) 可回收缓存线程池(newCachedThread ...

  3. JavaScript定时机制setTimeout与setInterval研究

    JavaScript的setTimeout与setInterval是两个很容易欺骗别人感情的方法,因为我们开始常常以为调用了就会按既定的方式执行, 我想不少人都深有同感, 例如 setTimeout( ...

  4. Executor(二)ThreadPoolExecutor、ScheduledThreadPoolExecutor 及 Executors 工厂类

    Executor(二)ThreadPoolExecutor.ScheduledThreadPoolExecutor 及 Executors 工厂类 Java 中的线程池类有两个,分别是:ThreadP ...

  5. java笔记--使用线程池优化多线程编程

    使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...

  6. java线程池,阿里为什么不允许使用Executors?

    带着问题 阿里Java代码规范为什么不允许使用Executors快速创建线程池? 下面的代码输出是什么? ThreadPoolExecutor executor = new ThreadPoolExe ...

  7. 阿里P7告诉你什么是java并发包、线程池、锁

    并发包 java.util.concurrent从jdk1.5开始新加入的一个包,致力于解决并发编程的线程安全问题,使用户能够更为快捷方便的编写多线程情况下的并发程序. 同步容器 同步容器只有包括Ve ...

  8. ExecutorService 线程池详解

    1.什么是ExecutorService,为什么要使用线程池? 许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务,每当一个请求到达就创建一个新线程,然后在新线程中为请求服务,但是频繁创建新 ...

  9. ScheduledThreadPoolExecutor中定时周期任务的实现源码分析

    ScheduledThreadPoolExecutor是一个定时任务线程池,相比于ThreadPoolExecutor最大的不同在于其阻塞队列的实现 首先看一下其构造方法: public Schedu ...

随机推荐

  1. World Cup

    World Cup Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Su ...

  2. 还在纠结 Flux 或 Relay,或许 Redux 更适合你

    重磅消息,Redux 1.0 发布,终于可以放心用于生产环境了! 在这个端应用技术膨胀的时代,每天都有一大堆框架冒出,号称解决了 XYZ 等一系列牛 X 的问题,然后过一段时间就不被提起了.但开发的应 ...

  3. CSS概要

    CSS概要 laiqun@msn.cn Contents 1. css的引入 2. css的选择器及效果图 3. css 盒模型 4. css 浮动 4.1. 浮动的作用: 4.2. 浮动的影响: 5 ...

  4. 安卓OpenGL入门

    1.先用一个GLSurfaceView作为画布,然后自定义一个Renderer继承自Renderer,把这个自定义的Renderer通过setRenderer()设置给GLSurfaceView就可以 ...

  5. -Swift.h not find

    亲测成功. 随便新建一个swift文件,xcode问是否生成xxx-Bridging-Header.h文件,点YES.再编译,问题解决. By default, the generated heade ...

  6. NET:交换机的背板带宽,交换容量,包转发率区别

    交换机的背板带宽,交换容量,包转发率区别 背板带宽指的是背板整个的交换容量,交换容量指cpu的交换容量,包转发指的是三层转发的容量 一.背板带宽 1.交换机背板带宽含义 交换机的背板带宽也叫背板容量, ...

  7. 笨方法学python--简介

    该章提到编程新手所需的三种最重要的技术:读和写,注重细节,发现不同. 读 和 写 即熟悉代码中的各种字符. 注 重 细 节 将例子一字不差地打出来,通过实践训练自己 发 现 不 同 这个是通过长年累月 ...

  8. 【同一直线最多点】 poj 1118+2606+2780

    poj 1118 #include<iostream> using namespace std; #define N 700 struct point {int x,y;} pnt[N]; ...

  9. Android开机动画启动流程

    android开机动画启动流程   从android的Surface Flinger服务启动分析知道,开机动画是在SurfaceFlinger实例通过调用startBootAnim()启动的. 下面我 ...

  10. Jenkins email-ext邮件通知模板

    http://blog.csdn.net/houyefeng/article/details/51344337 示例 以html格式发送送如下内容:邮件内容为项目名称.构建次数.触发原因.构建日志前1 ...