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. HDU4310:Hero

    Problem Description When playing DotA with god-like rivals and pig-like team members, you have to fa ...

  2. SharePoint 2010 应用url参数过滤列表视图数据(应用get办法过滤列表数据)

    名人名言:读活书,活读书,读书活.——郭沫若 题目其实不知道如何称呼才干合适大师的搜刮习惯.以便有类似题目经由过程百度或google可以搜刮到,其实就是在url后面添加参数过滤显示我们想要的成果,有人 ...

  3. weka简介

    1.weka的历史 1992年末,新西兰怀卡托大学计算机科学系Ian Written博士申请基金. 1993年获新西兰政府资助,并于同年开发出接口和基础架构. 1994年发布了第一个weka的内部版本 ...

  4. web.xml文件中的7个错误的安全配置

    web.xml文件中的7个错误的安全配置 关于Java的web.xml文件中配置认证和授权有大 量 的 文章.本文不再去重新讲解如何配置角色.保护web资源和设置不同类型的认证,让我们来看看web.x ...

  5. 笨方法学python--安装和准备

    1 下载并安装python http://python.org/download 下载python2.7. python2.7并不是python3.5的旧版本. python2现在应用较广,网上资料较 ...

  6. Petit FatFs

    FatFs is a generic FAT/exFAT file system module for small embedded systems. The FatFs module is writ ...

  7. mysql免安装版使用方法

    1.下载并解压mysql免安装版至自定义目录 2.添加环境变量 变量名:MYSQL_HOME 变量值:D:\Program Files\mysql-5.6.11-winx64 即为mysql的自定义解 ...

  8. Java学习笔记之static

    1.static可以用于修饰成员变量.方法以及块,并不会改变类中成员的权限修饰,如:private修饰的成员变量,类外只能类名或非私有方法调用,而不能使用对象名调用. 2.static方法和成员变量, ...

  9. Java中的一些术语的解释

    一  API(Application Programming Interface,应用程序编程接口) 简单来说,就是其他人开发出来一块程序,你想用,他会告诉你调用哪个函数,给这个函数传什么参数,然后又 ...

  10. 【最短路】 poj 2387

    #include <iostream> #include <stdlib.h> #include <limits.h> #include <string.h& ...