本人邮箱: <kco1989@qq.com>
欢迎转载,转载请注明网址 http://blog.csdn.net/tianshi_kco
github: https://github.com/kco1989/kco
代码已经所有托管github有须要的同窗自行下载java
同步工具都讲的差很少了,今天咱们换一下口味.讲一下定时任务Timer
吧.git
schedule(TimerTask task, long delay)
延时delay
ms后执行定时任务task
github
schedule(TimerTask task, Date time)
到达这个time
时间点执行定时任务task
微信
schedule(TimerTask task, long delay, long period)
延时delay
ms后执行定时任务task
,以后以period
ms为周期重复执行task
ide
schedule(TimerTask task, Date firstTime, long period)
到达这个time
时间点执行定时任务task
,以后以period
ms为周期重复执行task
工具
scheduleAtFixedRate(TimerTask task, long delay, long period)
延时delay
ms后执行定时任务task
,以后以period
ms为周期重复执行task
this
scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
到达这个time
时间点执行定时任务task
,以后以period
ms为周期重复执行task
spa
细心的人会发现带参数
period
的schedule
和scheduleAtFixedRate
的解释是同样,可是他们有什么区别
若是周期是30s,任务执行时间是8s,那么二者的执行效果是同样的
可是若是任务执行时间大于周期时间呢?scheduleAtFixedRate
会按照周期时间来,即无论任务执行多久,他都是周期一到就从新执行task
,
而schedule
的下一次开始执行时间是取决与上一次结束时间,若是任务执行时间大于周期时间呢,那么它会按照执行时间为周期执行任务task
.net
public class Demo1 { public static void main(String[] args) { Timer timer = new Timer(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("炸弹爆炸时间:" + dateFormat.format(new Date())); } }, 4000); System.out.println("炸弹安装时间:" + dateFormat.format(new Date())); } }
运行结果:code
炸弹安装时间:2016-10-31 20:00:25 炸弹爆炸时间:2016-10-31 20:00:29
public class Demo2 { public static void main(String[] args) throws ParseException { Timer timer = new Timer(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("炸弹爆炸时间:" + dateFormat.format(new Date())); } }, dateFormat.parse("2016-10-31 20:04:00")); System.out.println("炸弹安装时间:" + dateFormat.format(new Date())); } }
运行结果
炸弹安装时间:2016-10-31 20:03:11 炸弹爆炸时间:2016-10-31 20:04:00
public class Demo3 { public static void main(String[] args) { Timer timer = new Timer(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("炸弹爆炸时间:" + dateFormat.format(new Date())); } }, 2000,3000); System.out.println("炸弹安装时间:" + dateFormat.format(new Date())); } }
运行结果
炸弹安装时间:2016-10-31 20:05:46 炸弹爆炸时间:2016-10-31 20:05:48 炸弹爆炸时间:2016-10-31 20:05:51 炸弹爆炸时间:2016-10-31 20:05:54 炸弹爆炸时间:2016-10-31 20:05:57 炸弹爆炸时间:2016-10-31 20:06:00 炸弹爆炸时间:2016-10-31 20:06:03 炸弹爆炸时间:2016-10-31 20:06:06 ......
public class Demo4 { public static void main(String[] args) throws ParseException { Timer timer = new Timer(); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); timer.schedule(new TimerTask() { @Override public void run() { System.out.println("炸弹爆炸时间:" + dateFormat.format(new Date())); } }, dateFormat.parse("2016-10-31 20:08:30"), 2000); System.out.println("炸弹安装时间:" + dateFormat.format(new Date())); } }
运行结果
炸弹安装时间:2016-10-31 20:08:19 炸弹爆炸时间:2016-10-31 20:08:30 炸弹爆炸时间:2016-10-31 20:08:32 炸弹爆炸时间:2016-10-31 20:08:34 炸弹爆炸时间:2016-10-31 20:08:36 炸弹爆炸时间:2016-10-31 20:08:38 炸弹爆炸时间:2016-10-31 20:08:40 炸弹爆炸时间:2016-10-31 20:08:42 炸弹爆炸时间:2016-10-31 20:08:44 炸弹爆炸时间:2016-10-31 20:08:46 炸弹爆炸时间:2016-10-31 20:08:48 ......
period
的schedule
和scheduleAtFixedRate
的区别public class Demo5 { public static void main(String[] args) throws ParseException { Timer timer = new Timer(); SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); timer.schedule(new TimerTask() { @Override public void run() { try { Thread.sleep(4000); System.out.println("sub1执行时间:" + dataFormat.format(this.scheduledExecutionTime()) + " --> 当前时间:" + dataFormat.format(new Date())); } catch (InterruptedException e) { e.printStackTrace(); } } }, 0, 2000); } }
运行结果:
sub1执行时间:2016-10-31 08:12:05 --> 当前时间:2016-10-31 08:12:09 sub1执行时间:2016-10-31 08:12:09 --> 当前时间:2016-10-31 08:12:13 sub1执行时间:2016-10-31 08:12:13 --> 当前时间:2016-10-31 08:12:17 sub1执行时间:2016-10-31 08:12:17 --> 当前时间:2016-10-31 08:12:21 sub1执行时间:2016-10-31 08:12:21 --> 当前时间:2016-10-31 08:12:25 sub1执行时间:2016-10-31 08:12:25 --> 当前时间:2016-10-31 08:12:29 sub1执行时间:2016-10-31 08:12:29 --> 当前时间:2016-10-31 08:12:33 sub1执行时间:2016-10-31 08:12:33 --> 当前时间:2016-10-31 08:12:37 sub1执行时间:2016-10-31 08:12:37 --> 当前时间:2016-10-31 08:12:41 .....
如今将schedule
改成scheduleAtFixedRate
public class Demo6 { public static void main(String[] args) throws ParseException { Timer timer = new Timer(); SimpleDateFormat dataFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); timer.scheduleAtFixedRate(new TimerTask() { @Override public void run() { try { Thread.sleep(4000); System.out.println("sub1执行时间:" + dataFormat.format(this.scheduledExecutionTime()) + " --> 当前时间:" + dataFormat.format(new Date())); } catch (InterruptedException e) { e.printStackTrace(); } } }, 0, 2000); } }
运行结果:
sub1执行时间:2016-10-31 08:13:51 --> 当前时间:2016-10-31 08:13:55 sub1执行时间:2016-10-31 08:13:53 --> 当前时间:2016-10-31 08:13:59 sub1执行时间:2016-10-31 08:13:55 --> 当前时间:2016-10-31 08:14:03 sub1执行时间:2016-10-31 08:13:57 --> 当前时间:2016-10-31 08:14:07 sub1执行时间:2016-10-31 08:13:59 --> 当前时间:2016-10-31 08:14:11 sub1执行时间:2016-10-31 08:14:01 --> 当前时间:2016-10-31 08:14:15 sub1执行时间:2016-10-31 08:14:03 --> 当前时间:2016-10-31 08:14:19 .....
两个结果一对比,区别就很明显了
若是以为个人文章写的还过得去的话,有钱就捧个钱场,没钱给我捧我的场(帮我点赞或推荐一下)