schedule、scheduleAtFixedRate和scheduleWithFixedDelay的区别

最近使用java中的定时任务时,对于标题中的几个方法有点懵,故而记录一下。


schedule(commod,delay,unit) ,这个方法是说系统启动后,需要等待多久执行,delay是等待时间。只执行一次,没有周期性。

举个栗子:火箭发射,delay=10秒,发射准备好了之后,开始读秒:10,9,8,,,1,发射。piu~,任务完成,回家吃饭。


scheduleAtFixedRate(commod,initialDelay,period,unit),这个是以period为固定周期时间,按照一定频率来重复执行任务,initialDelay是说系统启动后,需要等待多久才开始执行。

举个栗子:高铁定时发车,过时不候;period为一天,以天为周期,优先保证任务执行的频率


scheduleWithFixedDelay(commod,initialDelay,delay,unit),这个是以delay为固定延迟时间,按照一定的等待时间来执行任务,initialDelay意义与上面的相同。

举个栗子:【这个例子相当的不好找。。。我后面的同事在玩炉石】就拿炉石传说来讲吧,假设他每玩一局休息10秒钟,然后再开始玩。每开新的一局,打完的时间不是固定的,但是间隔是固定的,就是delay=10秒,表现在时间轴上就是【第一局260秒】【休息10秒】【第二局150秒】 【休息10秒】...【第N局180秒】【休息10秒】。这个是优先保证任务执行的间隔


Java并发之ScheduledExecutorService(schedule、scheduleAtFixedRate、scheduleWithFixedDelay)

复制代码
 1 package com.thread.test.thread;
 2 import java.util.Timer;
 3 import java.util.TimerTask;
 4 import java.util.concurrent.*;
 5 import java.util.concurrent.locks.ReentrantLock;
 6 
 7 /**
 8  * schedule(Runnable command, long delay, TimeUnit unit)
 9  * @ command: 需要执行的任务
10  * @ delay:任务执行需要延迟的时间
11  * @ unit:时间单位
12  *
13  * 一次性执行任务,执行完成结束
14  *
15  * ScheduledExecutorService:
16  * scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
17  * @ Runnable command: 需要执行的任务
18  * @ long initialDelay:第一次执行延迟的时间
19  * @ long period:间隔周期
20  * @ TimeUnit unit
21  *
22  * 包含首次延迟的周期性执行任务,第一次执行:delay+period,第二次:delay+2*period,以此类推...
23  * 停止:异常停止执行,主动调用停止方法
24  * 如果某一个周期执行时间超过设定的period,则后续顺延
25  *
26  * scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)
27  * @ command: 需要执行的任务
28  * @ initialDelay:第一次执行延迟的时间
29  * @ delay:周期之间的延迟,间隔
30  * @ unit:前两个参数的单位
31  *
32  * 周期性执行任务:第一次执行:initialDelay+delay,第二次:initialDelay+2*delay,以此类推...
33  * 停止:异常停止执行,主动调用停止方法
34  * 不顺延
35  *
36  * Created by windwant on 2016/5/26.
37  */
38 public class MyExecutor {
39     public static void main(String[] args) {
40         testTimer();
41     }
42 
43     public static void testTimer(){
44         new Timer().schedule(new MyTimerTask(), 2000, 5000);
45     }
46 
47     public static void testExecutors(){
48         MyERunnable mer = new MyERunnable(5);
49 //        ExecutorService es = Executors.newCachedThreadPool();
50 //        ExecutorService es = Executors.newFixedThreadPool(2);
51         ScheduledExecutorService es = Executors.newScheduledThreadPool(2);
52         es.schedule(mer, 10000, TimeUnit.SECONDS.MILLISECONDS);
53         es.scheduleAtFixedRate(mer, 2, 10, TimeUnit.SECONDS);
54         es.scheduleWithFixedDelay(mer, 1, 5, TimeUnit.SECONDS);
55         es.shutdown();
56     }
57 }
58 
59 class MyERunnable implements Runnable{
60     private int num = 0;
61     MyERunnable(int num){
62         this.num = num;
63     }
64     public void run() {
65         ReentrantLock lock = new ReentrantLock();
66         try{
67             lock.lock();
68             for (int i = 0; i < num; i++) {
69                 System.out.println("current thread: " + Thread.currentThread().getName() + " num--" + i);
70                 Thread.sleep(1000);
71             }
72         }catch (Exception e){
73             e.printStackTrace();
74         }finally {
75             lock.unlock();
76         }
77     }
78 }
79 
80 class MyTimerTask extends TimerTask{
81 
82     @Override
83     public void run() {
84         System.out.println("timer task");
85     }
86 }
复制代码

项目地址:https://github.com/windwant/windwant-demo/tree/master/thread-demo


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值