定时且周期性的任务研究II--ScheduledThreadPoolExecutor
http://victorzhzh.iteye.com/blog/1011635
上一篇中我们看到了Timer的不足之处,本篇我们将围绕这些不足之处看看ScheduledThreadPoolExecutor是如何优化的。
为了研究方便我们需要两个类:
- public class Task1 implements Callable<String> {
- @Override
- public String call() throws Exception {
- String base = "abcdefghijklmnopqrstuvwxyz0123456789";
- Random random = new Random();
- StringBuffer sb = new StringBuffer();
- for (int i = 0; i < 10; i++) {
- int number = random.nextInt(base.length());
- sb.append(base.charAt(number));
- }
- System.out.println("Task1 running: " + new Date());
- return sb.toString();
- }
- }
生成含有10个字符的字符串,使用Callable接口目的是我们不再任务中直接输出结果,而主动取获取任务的结果
- public class LongTask implements Callable<String> {
- @Override
- public String call() throws Exception {
- System.out.println("LongTask running: "+new Date());
- TimeUnit.SECONDS.sleep(10);
- return "success";
- }
- }
长任务类,这里我们让任务沉睡10秒然后返回一个“success”
下面我们来分析一下ScheduledThreadPoolExecutor:
1、Timer中单线程问题是否在ScheduledThreadPoolExecutor中存在?
我们先来看一下面的程序:
- public class ScheduledThreadPoolExec {
- public static void main(String[] args) throws InterruptedException,
- ExecutionException {
- <strong><span style="color: #ff0000;">ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
- 2);</span>
- </strong>
- ScheduledFuture future1 = executor.schedule(new Task1(), 5,
- TimeUnit.SECONDS);
- ScheduledFuture future2 = executor.schedule(new LongTask(), 3,
- TimeUnit.SECONDS);
- BlockingQueue<ScheduledFuture> blockingQueue = new ArrayBlockingQueue<ScheduledFuture>(
- 2, true);
- blockingQueue.add(future2);
- blockingQueue.add(future1);
- System.out.println(new Date());
- while (!blockingQueue.isEmpty()) {
- ScheduledFuture future = blockingQueue.poll();
- if (!future.isDone())
- blockingQueue.add(future);
- else
- System.out.println(future.get());
- }
- System.out.println(new Date());
- executor.shutdown();
- }
- }
首先,我们定义了一个ScheduledThreadPoolExecutor它的池长度是2。接着提交了两个任务:第一个任务将延迟5秒执行,第二个任务将延迟3秒执行。我们建立了一个BlockingQueue,用它来存储了ScheduledFuture,使用ScheduledFuture可以获得任务的执行结果。在一个while循环中,我们每次将一个ScheduledFuture从队列中弹出,验证它是否被执行,如果没有被执行则再次将它加入队列中,如果被执行了,这使用ScheduledFuture的get方法获取任务执行的结果。看一下执行结果:
- Thu Apr 21 19:23:02 CST 2011
- LongTask running: Thu Apr 21 19:23:05 CST 2011
- Task1 running: Thu Apr 21 19:23:07 CST 2011
- h1o2wd942e
- success
- Thu Apr 21 19:23:15 CST 2011
我们看到长任务先运行,因为长任务只等待了3秒,然后是输出字符串的任务运行,两个任务开始时间相差2秒,而先输出了字符串而后才是长任务运行的结果success,最后我们查看一下整体的开始和结束时间相差了13秒。
这说明在ScheduledThreadPoolExecutor中它不是以一个线程运行任务的,而是以多个线程,如果用一个线程运行任务,那么长任务运行完之前是不会运行输出字符串任务的。其实这个“多个任务“是我们自己指定的注意一下标红的代码,如果我们把这行代码改为:
- ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1);
那么运行结果就会发生变化,
- Thu Apr 21 19:36:56 CST 2011
- LongTask running: Thu Apr 21 19:36:59 CST 2011
- success
- Task1 running: Thu Apr 21 19:37:09 CST 2011
- y981iqd0or
- Thu Apr 21 19:37:09 CST 2011
这时其实和使用Timer运行结果是一样的。任务是在一个线程里顺序执行的。
2、Timer中一但有运行时异常报出后续任务是否还会正常运行?
为了研究这个问题,我们还是需要一个能够抛出异常的任务,如下:
- public class TimerExceptionTask extends TimerTask {
- @Override
- public void run() {
- System.out.println("TimerExceptionTask: "+new Date());
- throw new RuntimeException();
- }
- }
我们对上面运行任务的代码做一点点小小的修改,先运行两个抛出异常的任务,如下:
- public class ScheduledThreadPoolExec {
- public static void main(String[] args) throws InterruptedException,
- ExecutionException {
- ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(
- 2);
- ScheduledFuture future1 = executor.schedule(new Task1(), 5,
- TimeUnit.SECONDS);
- ScheduledFuture future2 = executor.schedule(new LongTask(), 3,
- TimeUnit.SECONDS);
- <strong><span style="color: #ff0000;">executor.schedule(new TimerExceptionTask(), 1, TimeUnit.SECONDS);
- executor.schedule(new TimerExceptionTask(), 2, TimeUnit.SECONDS);</span>
- </strong>
- BlockingQueue<ScheduledFuture> blockingQueue = new ArrayBlockingQueue<ScheduledFuture>(
- 2, true);
- blockingQueue.add(future2);
- blockingQueue.add(future1);
- System.out.println(new Date());
- while (!blockingQueue.isEmpty()) {
- ScheduledFuture future = blockingQueue.poll();
- if (!future.isDone())
- blockingQueue.add(future);
- else
- System.out.println(future.get());
- }
- System.out.println(new Date());
- executor.shutdown();
- }
- }
注意,标红的代码,如果这两个代码抛出错误后会影响后续任务,那么就应该在此终止,但是看一下结果,
- Thu Apr 21 19:40:15 CST 2011
- TimerExceptionTask: Thu Apr 21 19:40:16 CST 2011
- TimerExceptionTask: Thu Apr 21 19:40:17 CST 2011
- LongTask running: Thu Apr 21 19:40:18 CST 2011
- Task1 running: Thu Apr 21 19:40:20 CST 2011
- v5gcf01iiz
- success
- Thu Apr 21 19:40:28 CST 2011
后续任务仍然执行,可能会有朋友说:“你上面的池设置的是2,所以很有可能是那两个抛出异常的任务都在同一个线程中执行,而另一个线程执行了后续的任务”。那我们就把ScheduledThreadPoolExecutor设置成1看看,结果如下:
- Thu Apr 21 19:43:00 CST 2011
- TimerExceptionTask: Thu Apr 21 19:43:01 CST 2011
- TimerExceptionTask: Thu Apr 21 19:43:02 CST 2011
- LongTask running: Thu Apr 21 19:43:03 CST 2011
- success
- Task1 running: Thu Apr 21 19:43:13 CST 2011
- 33kgv8onnd
- Thu Apr 21 19:43:13 CST 2011
后续任务也执行了,所以说ScheduledThreadPoolExecutor不会像Timer那样有线程泄漏现象。
对于周期性执行和Timer很类似这里就不再举例了。
定时且周期性的任务研究II--ScheduledThreadPoolExecutor的更多相关文章
- 定时且周期性的任务研究I--Timer
很多时候我们希望任务可以定时的周期性的执行,在最初的JAVA工具类库中,通过Timer可以实现定时的周期性的需求,但是有一定的缺陷,例如:Timer是基于绝对时间的而非支持相对时间,因此Timer对系 ...
- (CSDN 迁移) JAVA多线程实现-支持定时与周期性任务的线程池(newScheduledThreadPool)
前几篇文章中分别介绍了 单线程化线程池(newSingleThreadExecutor) 可控最大并发数线程池(newFixedThreadPool) 可回收缓存线程池(newCachedThread ...
- JavaScript定时机制setTimeout与setInterval研究
JavaScript的setTimeout与setInterval是两个很容易欺骗别人感情的方法,因为我们开始常常以为调用了就会按既定的方式执行, 我想不少人都深有同感, 例如 setTimeout( ...
- Executor(二)ThreadPoolExecutor、ScheduledThreadPoolExecutor 及 Executors 工厂类
Executor(二)ThreadPoolExecutor.ScheduledThreadPoolExecutor 及 Executors 工厂类 Java 中的线程池类有两个,分别是:ThreadP ...
- java笔记--使用线程池优化多线程编程
使用线程池优化多线程编程 认识线程池 在Java中,所有的对象都是需要通过new操作符来创建的,如果创建大量短生命周期的对象,将会使得整个程序的性能非常的低下.这种时候就需要用到了池的技术,比如数据库 ...
- java线程池,阿里为什么不允许使用Executors?
带着问题 阿里Java代码规范为什么不允许使用Executors快速创建线程池? 下面的代码输出是什么? ThreadPoolExecutor executor = new ThreadPoolExe ...
- 阿里P7告诉你什么是java并发包、线程池、锁
并发包 java.util.concurrent从jdk1.5开始新加入的一个包,致力于解决并发编程的线程安全问题,使用户能够更为快捷方便的编写多线程情况下的并发程序. 同步容器 同步容器只有包括Ve ...
- ExecutorService 线程池详解
1.什么是ExecutorService,为什么要使用线程池? 许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务,每当一个请求到达就创建一个新线程,然后在新线程中为请求服务,但是频繁创建新 ...
- ScheduledThreadPoolExecutor中定时周期任务的实现源码分析
ScheduledThreadPoolExecutor是一个定时任务线程池,相比于ThreadPoolExecutor最大的不同在于其阻塞队列的实现 首先看一下其构造方法: public Schedu ...
随机推荐
- js-call、apply
这里主要就是做一些前人的总结,有时候会有自己的看法,首先把定义说一下 1.方法定义 call方法: 语法:call([thisObj[,arg1[, arg2[, [,.argN]]]]]) 定义 ...
- Inno Setup入门(二十一)——Inno Setup类参考(7)
Install Setup 2013-02-02 11:31 378人阅读 评论(0) 收藏 举报 复选框 复选框(CheckBox)用于多个并不互斥的几个选项中作出一个或者多选择,例如字体可以有粗体 ...
- c++之模板
. 函数模板 普通函数 void Swap(int &, int &); 模板函数 template <typename T> void Swap(T &, T & ...
- Java 类的加载过程(阿里面试题)
问以下程序打印出什么内容: 问题及解析如下: /** * 加载方法不等于执行方法,初始化变量则会赋值 * 类加载顺序应为 加载静态方法-初始化静态变量-执行静态代码块 * 实例化时 先加载非静态方法- ...
- select用法
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...
- 关于spring的注解方式注入默认值(转) -- 首字母小写
1.是首字母小写 比如 UserAction对应的id是userAction 可以通过ApplicationContext 对象的act.getBean("userAction") ...
- wpf计时器
wpf应用程序间隔一段时间触发一个事件,代码如下: public MainWindow() { InitializeComponent(); DispatcherTimer timer = new D ...
- 【一题多解】 map 二分 hash trie poj 2503
各种方式解这道题!! 利用map 超时了 #include <iostream> #include <string> #include <map> using na ...
- TabControl选项卡
<Grid> <TabControl Name="tabControl1"> <TabItem Name="tabItem1"&g ...
- iOS字符串转化成CGFloat
NSString *str = @"abc"; [str floatValue];