当前位置: 移动技术网 > IT编程>开发语言>Java > 基于ScheduledExecutorService的两种方法(详解)

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

2019年07月19日  | 移动技术网IT编程  | 我要评论

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

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

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

1.scheduleatfixedrate方法

例子:

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()));
  }
}

运行结果:

++++++++++++++++++++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方法

例子:

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()));
  }
}

运行结果:

++++++++++++++++++++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的两种方法(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网