java定时任务接口ScheduledExecutorService

一、ScheduledExecutorService 设计思想

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

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

ScheduledExecutorService 中两种最常用的调度方法 ScheduleAtFixedRate 和 ScheduleWithFixedDelay。

ScheduleAtFixedRate :每次执行时间为上一次任务开始起向后推一个时间间隔。ScheduleAtFixedRate 是基于固定时间间隔进行任务调度

ScheduleWithFixedDelay: 每次执行时间为上一次任务结束起向后推一个时间间隔。ScheduleWithFixedDelay 取决于每次任务执行的时间长短,是基于不固定时间间隔进行任务调度。

二、示例

ScheduleAtFixedRate

1、任务

import java.util.Date;

class Task implements Runnable {

	private String taskName;

	Task(String taskName) {
		this.taskName = taskName;
	}

	@Override
	public void run() {
		System.out.println(taskName + " is running.Time:" + new Date());
	}
}

2、执行类

import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;
import java.util.Date;

public class TaskMain {
	public static void main(String[] args) {
		// 创建线程池
		ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
		// 初始延时时间
		long initialDelay = 3;
		// 任务执行间隔
		long period = 1;
		System.out.println("任务开始:" + new Date());
		// 从现在开始3秒钟之后,每隔1秒钟执行一次Task1
		service.scheduleAtFixedRate(new Task("Task1"), initialDelay, period, TimeUnit.SECONDS);
	}
}

3、执行结果

任务开始:Sun Jan 27 16:24:30 CST 2019
Task1 is running.Time:Sun Jan 27 16:24:33 CST 2019
Task1 is running.Time:Sun Jan 27 16:24:34 CST 2019
Task1 is running.Time:Sun Jan 27 16:24:35 CST 2019
Task1 is running.Time:Sun Jan 27 16:24:36 CST 2019
Task1 is running.Time:Sun Jan 27 16:24:37 CST 2019
Task1 is running.Time:Sun Jan 27 16:24:38 CST 2019

ScheduleWithFixedDelay

1、任务

package com.learn.schedule;

import java.util.Date;

class Task implements Runnable {

	private String taskName;

	Task(String taskName) {
		this.taskName = taskName;
	}

	@Override
	public void run() {
		try {
			// 模拟业务执行1s
			Thread.sleep(1000L);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(taskName + " is running.Time:" + new Date());
	}
}

2、执行类

import java.util.concurrent.TimeUnit;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.Executors;
import java.util.Date;

public class TaskMain {
	public static void main(String[] args) {
		// 创建线程池
		ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
		// 初始延时时间
		long initialDelay = 3;
		// 任务执行间隔
		long period = 1;
		System.out.println("任务开始:" + new Date());

		// 从现在开始3秒钟之后,每隔2秒钟执行一次Task2
		service.scheduleWithFixedDelay(new Task("Task2"), initialDelay, period, TimeUnit.SECONDS);
	}
}

3、执行结果

任务开始:Sun Jan 27 16:29:52 CST 2019
Task2 is running.Time:Sun Jan 27 16:29:56 CST 2019
Task2 is running.Time:Sun Jan 27 16:29:58 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:00 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:02 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:04 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:06 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:08 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:10 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:12 CST 2019
Task2 is running.Time:Sun Jan 27 16:30:14 CST 2019

 

你可能感兴趣的:(Java开发)