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. restlet不能接受angular post过来的数据

    修改header create: { method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded'} }

  2. 用tomcat6自定义域名

    第一步:tomcat配置 修改server.xml文件 8080端口 更改为 80端口 并在<Host name="localhost"  appBase="web ...

  3. c# 快速验证代理IP是否有用

    public void yanzhen(string str, int port) { WebProxy proxyObject = new WebProxy(str, port);//str为IP地 ...

  4. make module失败的原因cc1: error: unrecognized command line option “-m64

    cc1: error: unrecognized command line option "-m64"cc1: error: unrecognized command line o ...

  5. sql语句按月份统计查询

    select year(createdate) 年,month(createdate) 月,count(1) from public_cms_arcwhere (userid in (select i ...

  6. Asp:Cookies应用指南

    实际上,在web开发中,cookie仅仅是一个文本文件,当用户访问站点时,它就被存储在用户使用的计算机上,其中,保存了 一些信息,当用户日后再次访问这个站点时,web可以将这些信息提取出来.    尽 ...

  7. String s = new String("aa") 创建了几个对象?

    1 最近几个同学面试的时候出现了这样一个问题 刚听到这个题目的时候的确是不知所措: 经过网上的查找和自己的理解来解释一下这个题目的答案 答案是: 为什么呢??? 1 实现我们都知道创建实例有两种方法 ...

  8. java中对List中对象排序实现

    package com.test; import java.util.ArrayList; import java.util.Collections; import java.util.Compara ...

  9. WPF(ContentControl和ItemsControl)

    WPF(ContentControl和ItemsControl) 2013-04-01 16:25 2188人阅读 评论(0) 收藏 举报  分类: .Net(C#)(31)  WPF(25)  版权 ...

  10. HDU1492/The number of divisors(约数) about Humble Numbers

    题目连接 The number of divisors(约数) about Humble Numbers Time Limit: 2000/1000 MS (Java/Others) Memory L ...