Timer和ScheduledExecutorService区别
Timer特点:

import java.util.Date;
import java.util.Timer;
import java.util.TimerTask; public class TimerTest extends TimerTask { private String jobName = "" ; public TimerTest(String jobName) {
super();
this.jobName = jobName;
} int count = 0; @Override
public void run() {
System. out.println(jobName + " " + new Date() + " beep " + (++count));
try {
if (jobName .equals("job1" )) {
Thread. sleep(1000);
throw new RuntimeException();
} } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
Timer timer = new Timer();
long period = 2000;
// 从现在开始 1 秒钟之后,每隔 2秒钟执行一次 job1
timer.schedule( new TimerTest("job1" ), 0, period);
// 每隔 2 秒钟执行一次 job2
timer.schedule( new TimerTest("job2" ), 0, period);
}
}
执行结果
job1 Tue Apr 08 23:37:17 CST 2014 beep 1
Exception in thread "Timer-0" java.lang.RuntimeException
at TimerTest.run( TimerTest.java:20)
at java.util.TimerThread.mainLoop( Timer.java:512)
at java.util.TimerThread.run( Timer.java:462)
.png)
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.Date;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture; public class TestScheduledThread extends TimerTask { private String jobName = "" ; public TestScheduledThread(String jobName) {
super();
this.jobName = jobName;
} int count = 0; @Override
public void run() {
System. out.println(jobName + " " + new Date() + " beep " + (++count));
try {
if (jobName .equals("job1" )) {
Thread. sleep(1000);
throw new RuntimeException();
} } catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public static void main(String[] args) {
final ScheduledExecutorService scheduler = Executors
. newScheduledThreadPool(2);
//每隔2秒运行一次
final ScheduledFuture beeperHandle = scheduler.scheduleAtFixedRate(
new TimerTest("job1" ), 0, 2, SECONDS);
//每隔2秒运行一次
final ScheduledFuture beeperHandle2 = scheduler.scheduleAtFixedRate(
new TimerTest("job2" ), 0, 2, SECONDS);
}
}
Timer和ScheduledExecutorService区别的更多相关文章
- 任务调度的方式:Timer、ScheduledExecutorService、spring task、quartz、XXL-JOB、Elastic-Job
任务调度 定时任务调度:基于给定的时间点.给定的时间间隔.给定的执行次数自动执行的任务. Timer 介绍 Timer,简单无门槛,一般也没人用. Timer位于java.util包下,其内部包含且仅 ...
- java定时任务Timer与ScheduledExecutorService<转>
在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现.下面LZ就其原理.实例以及Timer缺陷三个方面来解析java Timer定时器. ...
- Java 多线程之Timer与ScheduledExecutorService
1.Timer管理延时任务的缺陷 a.以前在项目中也经常使用定时器,比如每隔一段时间清理项目中的一些垃圾文件,每个一段时间进行数据清洗:然而Timer是存在一些缺陷的,因为Timer在执行定时任务时只 ...
- Timer与ScheduledExecutorService间的抉择
java.util.Timer计时器有管理任务延迟执行("如1000ms后执行任务")以及周期性执行("如每500ms执行一次该任务").但是,Timer存在一 ...
- 【高并发】ScheduledThreadPoolExecutor与Timer的区别和简单示例
JDK 1.5开始提供ScheduledThreadPoolExecutor类,ScheduledThreadPoolExecutor类继承ThreadPoolExecutor类重用线程池实现了任务的 ...
- 实例分析Scheduled Thread Pool Executor与Timer的区别
摘要:JDK 1.5开始提供Scheduled Thread PoolExecutor类,Scheduled Thread Pool Executor类继承Thread Pool Executor类重 ...
- C#中Timer使用及解决重入问题
C#中Timer使用及解决重入问题 ★介绍 首先简单介绍一下timer,这里所说的timer是指的System.Timers.timer,顾名思义,就是可以在指定的间隔是引发事件.官方介绍在这里,摘抄 ...
- java Timer 使用小结
Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务.这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少. 任务的调用通过起的子线程进 ...
- Java Timer及TimerTarsk(摘自网络)
Java自带的java.util.Timer类,通过调度一个java.util.TimerTask任务. 这种方式可以让程序按照某一个频度执行,但不能指定时间运行.用的较少.任务的调用通过起的子线程进 ...
随机推荐
- small test on 5.30 night T1
数学题使劲推就对了. 让我们设 g(x) = ∑ C(i,x) * b^i ,然后后面验算了一张纸QWQ,懒得再打一遍了,回家我就把这张演算纸补上QWQ,先上代码. #include<cstd ...
- 【点分治】bzoj2152 聪聪可可
模板题. #include<cstdio> #include<algorithm> #include<cstring> #include<cmath> ...
- 【动态规划】【记忆化搜索】CODEVS 1010 过河卒 2002年NOIP全国联赛普及组
f(i,j)=f(i-1,j)+f(i,j-1),显然可以暴力递归求解,但是很多重复的状态,所以可以记忆下来. 注意障碍点和边界的特判. #include<cstdio> #include ...
- SQL函数学习(十六):STUFF()函数
16.STUFF()函数 STUFF()函数用于删除指定长度的字符,并可以在制定的起点处插入另一组字符. 16.1 STUFF()函数语法 select stuff(列名,开始位置,长度,替代字符串) ...
- iOS开发——随机数的使用
1).arc4random() 比较精确不需要生成随即种子 使用方法 : 通过arc4random() 获取0到x-1之间的整数的代码如下: ...
- Could not instantiate bean class [org.springframework.data.domain.Pageable]: Specified class is an interface解决方案
原文:http://pimin.net/archives/432 环境:Eclipse LUNA.Spring 4.1.1.或Spring 4.3.3.Spring Data Elasticsearc ...
- android多线程-AsyncTask之工作原理深入解析(上)
关联文章: Android 多线程之HandlerThread 完全详解 Android 多线程之IntentService 完全详解 android多线程-AsyncTask之工作原理深入解析(上) ...
- 剪切Postscript图片中的多余边框
最近用plplot画图,其cairo ps库生成的ps图片总是不能合理地剪切掉多余的边框,于是乎自己写了一个小脚本epscrop,用修改ps图的BoundingBox. #!/bin/bash # c ...
- GDUT决赛题解
决赛,我自我认为题目难度更大,反而我的心态更好了. 由于放轻松的时候反而效果更好,跟昨天的观点一样,凡是可以1A的,才算这题做得好. A.数目不大,关键是看懂题(我自己连输入输出是什么都不清楚.... ...
- Gradle项目学习 & HttpAsyncClient学习 & CountDownLatch学习
装了Intellij,就是装了Gradle. 新建一个Gradle项目.然后下面这个页面要勾选上面两项: Use auto-import和Create directories for empty co ...