资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

基于ScheduledExecutorService的方法有哪些

这篇文章主要介绍基于ScheduledExecutorService的方法有哪些,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!

创新互联是一家专注于做网站、网站设计与策划设计,郓城网站建设哪家好?创新互联做网站,专注于网站建设十余年,网设计领域的专业建站公司;建站业务涵盖:郓城等地区。郓城做网站价格咨询:18980820575

开发中,往往遇到另起线程执行其他代码的情况,用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的方法有哪些”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!


分享题目:基于ScheduledExecutorService的方法有哪些
浏览地址:http://cdkjz.cn/article/gipeoi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220