服务器之家

服务器之家 > 正文

基于ScheduledExecutorService的两种方法(详解)

时间:2021-01-23 12:34     来源/作者:面条啊Andrew

开发中,往往遇到另起线程执行其他代码的情况,用java定时任务接口ScheduledExecutorService来实现。

ScheduledExecutorService是基于线程池设计的定时任务类,每个调度任务都会分配到线程池中的一个线程去执行,也就是说,任务是并发执行,互不影响。

注意,只有当调度任务来的时候,ScheduledExecutorService才会真正启动一个线程,其余时间ScheduledExecutorService都是处于轮询任务的状态。

1.scheduleAtFixedRate方法

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
public class ScheduleAtFixedRateDemo {
 
  public static void main(String[] args) {
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    SimpleDateFormat df =
        new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    
    executorService.scheduleAtFixedRate(new Runnable(){
 
      @Override
      public void run() {
        System.out.println("++++++++++++++++++++thread:" + df.format(new Date()));
      }
      
    }, 2, 3, TimeUnit.SECONDS);
    System.out.println("++++++++++++++++++++main:" + df.format(new Date()));
  }
}

运行结果:

?
1
2
3
4
5
6
++++++++++++++++++++main:2017-10-20 15:20:52
++++++++++++++++++++thread:2017-10-20 15:20:54
++++++++++++++++++++thread:2017-10-20 15:20:57
++++++++++++++++++++thread:2017-10-20 15:21:00
++++++++++++++++++++thread:2017-10-20 15:21:03
++++++++++++++++++++thread:2017-10-20 15:21:06

可以看出来,在2s后,子线程开始执行,并且每过3s轮询执行一次。

2.scheduleWithFixedDelay方法

例子:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
/**
 * ScheduleWithFixedDelay的用法
 * @author Administrator
 *
 */
public class ScheduleWithFixedDelayDemo {
 
  public static void main(String[] args) {
    ScheduledExecutorService executorService = Executors.newScheduledThreadPool(1);
    SimpleDateFormat df =
      new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
    
    executorService.scheduleWithFixedDelay(new Runnable(){
      @Override
      public void run() { 
        System.out.println("++++++++++++++++++++thread:" + df.format(new Date()));
      }
      
    }, 2, 3, TimeUnit.SECONDS);
    
    System.out.println("++++++++++++++++++++main:" + df.format(new Date()));
  }
}

运行结果:

?
1
2
3
4
5
++++++++++++++++++++main:2017-10-20 15:24:32
++++++++++++++++++++thread:2017-10-20 15:24:34
++++++++++++++++++++thread:2017-10-20 15:24:37
++++++++++++++++++++thread:2017-10-20 15:24:40
++++++++++++++++++++thread:2017-10-20 15:24:43

3.两个区别

ScheduleAtFixedRate每次执行时间为上一次任务开始起向后推一个时间间隔,即每次执行时间为initialDelay,initialDelay+period,initialDelay+2*period。。。。。

ScheduleWithFixedDelay每次执行时间为上一次任务结束起向后推一个时间间隔,即每次执行时间为:initialDelay,initialDelay+executeTime+delay,initialDelay+2*executeTime+2*delay。。。。。

由此可见,ScheduleAtFixedRate是基于固定时间间隔进行任务调度,ScheduleWithFixedDelay取决于每次任务执行的时间长短,是基于不固定时间间隔进行任务调度。

以上这篇基于ScheduledExecutorService的两种方法(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:http://www.cnblogs.com/miantiaoandrew/p/7699833.html

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享
歪歪漫画vip账号共享2020_yy漫画免费账号密码共享 2020-04-07
返回顶部