开发中,往往遇到另起线程执行其他代码的情况,用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的两种方法的更多相关文章

  1. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  2. android 之 启动画面的两种方法

    现在,当我们打开任意的一个app时,其中的大部分都会显示一个启动界面,展示本公司的logo和当前的版本,有的则直接把广告放到了上面.启动画面的可以分为两种设置方式:一种是两个Activity实现,和一 ...

  3. [转载]C#读写txt文件的两种方法介绍

    C#读写txt文件的两种方法介绍 by 大龙哥 1.添加命名空间 System.IO; System.Text; 2.文件的读取 (1).使用FileStream类进行文件的读取,并将它转换成char ...

  4. php如何防止图片盗用/盗链的两种方法(转)

    图片防盗链有什么用? 防止其它网站盗用你的图片,浪费你宝贵的流量.本文章向大家介绍php防止图片盗用/盗链的两种方法 Apache图片重定向方法 设置images目录不充许http访问 Apache服 ...

  5. WPF程序将DLL嵌入到EXE的两种方法

    WPF程序将DLL嵌入到EXE的两种方法 这一篇可以看作是<Visual Studio 版本转换工具WPF版开源了>的续,关于<Visual Studio 版本转换工具WPF版开源了 ...

  6. MongoDB实现分页(两种方法)

    1.插入实验数据 偷懒用下samus,100条. ; i < ; i++) { Document doc = new Document(); doc["ID"] = i; d ...

  7. css:图标与文字对齐的两种方法

    (好久没写博客了,这几个月的积累比较零碎,记在本子上,现在开始整理归类) 在平时写页面的过程中,常遇到要把小图标与文字对齐的情况.比如: 总结了两种方法,代码量都比较少. 第一种 对img设置竖直方向 ...

  8. .net中创建xml文件的两种方法

    .net中创建xml文件的两种方法 方法1:根据xml结构一步一步构建xml文档,保存文件(动态方式) 方法2:直接加载xml结构,保存文件(固定方式) 方法1:动态创建xml文档 根据传递的值,构建 ...

  9. 两种方法设置disabled属性

    //两种方法设置disabled属性 $('#fileup').attr("disabled",true); $('#fileup').attr("disabled&qu ...

随机推荐

  1. 【文文殿下】[CEOI2004]锯木厂选址 题解

    题解 我们枚举建厂的位置,发现有个\(n^2\)的DP.随手搞个斜率优化到\(O(n)\). #include<bits/stdc++.h> using namespace std; ty ...

  2. mac终端常用命令

    1.du #查看文件目录大小 示例:查看DataCenter目录下所有文件/文件夹的大小 everSeeker:DataCenter pingping$ -h .9G ./Books 1.2M ./C ...

  3. 深入字节码理解invokeSuper无限循环的原因

    来一段简单的cglib代码 public class SampleClass { public void test(){ System.out.println("hello world&qu ...

  4. iOS开发-实现相机app的方法[转载自官方]

    This brief code example to illustrates how you can capture video and convert the frames you get to U ...

  5. SpaceSyntax【空间句法】之DepthMapX学习:第三篇 软件介绍与一般分析流程图

    上篇讲啥来着?好像讲了数据的输入以及一些核心的概念.这篇讲软件长什么样,做那几种分析的步骤如何. 博客园/B站/知乎/CSDN @秋意正寒(我觉得这一篇肯定很多盗图的,那么我在版头加个本篇地址吧)ht ...

  6. mongo数据查询操作

    本文来源于 :Stephen Liu 1.  基本查询:     构造查询数据.    > db.test.findOne()    {         "_id" : Ob ...

  7. (转)Python Mixins 机制

    原文:https://github.com/dengshuan/notes/blob/master/techs/python-mixins.org https://blog.csdn.net/u012 ...

  8. 为什么研发团队不适合量化KPI的绩效考核?

    研发团队(如果不是外包,不是机械性的活动)如果进行的是creative的有创造性的智力活动,那么应该不适合用量化KPI的绩效考核和激励,不应该用工时.bug数(难度大的bug可能多,测试人员可能没有经 ...

  9. Linux的Bash Shell详解

    一.Bash Shell概述 1.什么是bash         bash是Bourne Again Shell的简称,是从unix系统中的sh发展而来的,是用户和Linux内核交互的工具,用户通过b ...

  10. Spring Boot + Spring Cloud 实现权限管理系统 后端篇(八):MyBatis分页功能实现

    使用Mybatis时,最头痛的就是写分页,需要先写一个查询count的select语句,然后再写一个真正分页查询的语句,当查询条件多了之后,会发现真不想花双倍的时间写 count 和 select,幸 ...