博客
关于我
线程池-----ScheduledExecutorService实现定时任务
阅读量:334 次
发布时间:2019-03-04

本文共 2831 字,大约阅读时间需要 9 分钟。

接口scheduleAtFixedRate原型定义及参数说明:

public ScheduledFuture
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit);

command:执行线程;

initialDelay:初始化延时;
period:两次开始执行最小间隔时间;
unit:计时单位;

1、使用scheduleAtFixedRate()方法实现周期性执行

如:每隔100毫秒,执行一次run任务,得到执行后的打印::

import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;public class ScheduledExecutorServiceTest {       public static void main(String[] args) {           ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();        executorService.scheduleAtFixedRate(new Runnable() {               @Override            public void run() {                   System.out.println("run "+ System.currentTimeMillis());            }        }, 0, 100, TimeUnit.MILLISECONDS);    }

2、使用scheduleAtFixedRate()方法实现周期性定时任务

有时候我们希望一个任务被安排在凌晨3点(访问较少时)周期性的执行一个比较耗费资源的任务,可以使用下面方法设定每天在固定时间执行一次任务。

import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date;import java.util.concurrent.Executors;import java.util.concurrent.ScheduledExecutorService;import java.util.concurrent.TimeUnit;@Componentpublic class ExecutorTest {       public static final ScheduledExecutorService service = Executors.newScheduledThreadPool(4);    public static void main(String[] args) {           long oneDay = 24 * 60 * 60 * 1000;        long initDelay  = getTimeMillis("03:00:00") - System.currentTimeMillis();        initDelay = initDelay > 0 ? initDelay : oneDay + initDelay;        service.scheduleAtFixedRate(new MyScheduled1(),initDelay,oneDay, TimeUnit.MILLISECONDS);        service.scheduleAtFixedRate(new MyScheduled2(),initDelay,oneDay,TimeUnit.MILLISECONDS);    }/** * 获取指定时间对应的毫秒数 * @param time "HH:mm:ss" * @return */private static long getTimeMillis(String time) {   	try {   		DateFormat dateFormat = new SimpleDateFormat("yy-MM-dd HH:mm:ss");		DateFormat dayFormat = new SimpleDateFormat("yy-MM-dd");		Date curDate = dateFormat.parse(dayFormat.format(new Date()) + " " + time);		return curDate.getTime();	} catch (ParseException e) {   		e.printStackTrace();	}	return 0;

自定义线程:

public class MyScheduled1 implements Runnable{       @Override    public void run() {           System.out.println("my scheduled 01-----");    }}public class MyScheduled2 implements Runnable {       @Override    public void run() {           System.out.println("my scheduled 02 ------");    }}

注意与另一个接口的区别:

接口scheduleWithFixedDelay原型定义及参数说明

public ScheduledFuture
scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit);

command:执行线程

initialDelay:初始化延时
period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间)
unit:计时单位

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

转载地址:http://xybe.baihongyu.com/

你可能感兴趣的文章
上周热点回顾(6.16-6.22)
查看>>
上周热点回顾(6.23-6.29)
查看>>
上周热点回顾(10.20-10.26)
查看>>
上周热点回顾(2.16-2.22)
查看>>
上周热点回顾(3.2-3.8)
查看>>
[网站公告]3月10日23:00-4:00阿里云SLB升级,会有4-8次连接闪断
查看>>
.NET跨平台之旅:借助ASP.NET 5 Beta5的新特性显示CLR与操作系统信息
查看>>
上周热点回顾(7.27-8.2)
查看>>
上周热点回顾(9.28-10.4)
查看>>
上周热点回顾(3.28-4.3)
查看>>
上周热点回顾(5.2-5.8)
查看>>
上周热点回顾(5.9-5.15)
查看>>
上周热点回顾(8.8-8.14)
查看>>
.NET跨平台之旅:将示例站点升级至 .NET Core 1.1 Preview 1
查看>>
上周热点回顾(1.16-1.22)
查看>>
上周热点回顾(1.23-1.29)
查看>>
上周热点回顾(3.20-3.26)
查看>>
上周热点回顾(4.24-4.30)
查看>>
[故障公告]博客站点1台负载均衡遭遇流量攻击,造成联通与移动用户无法正常访问
查看>>
上周热点回顾(5.1-5.7)
查看>>