java中基于线程池的任务调度设计
2014-04-11 10:45
260 查看
基于Timer类的设计缺陷,java5中引入的ScheduledExecutor线程池设计。其设计思想是,每一个被调度的任务都会由线程池中一个线程去执行,因此任务是并发执行的,相互之间不会受到干扰。需要注意的是,只有当任务的执行时间到来时,ScheduedExecutor
才会真正启动一个线程,其余时间 ScheduledExecutor 都是在轮询任务的状态。
实例代码:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorTest implements Runnable {
private String jobName = "";
public ScheduledExecutorTest(String jobName) {
this.jobName = jobName;
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
System.out.println("execute " + jobName);
}
public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors
.newScheduledThreadPool(10);
long initialDelay1 = 1;
long period1 = 1;
// 从现在开始1秒钟之后,每隔1秒钟执行一次job1
scheduledExecutorService.scheduleAtFixedRate(new ScheduledExecutorTest(
"job1"), initialDelay1, period1, TimeUnit.SECONDS);
long initialDelay2 = 1;
long delay2 = 1;
// 从现在开始2秒钟之后,每隔2秒钟执行一次job2
scheduledExecutorService.scheduleWithFixedDelay(
new ScheduledExecutorTest("job2"), initialDelay2, delay2,
TimeUnit.SECONDS);
}
}
运行结果:
execute job1
execute job2
execute job1
execute job2
ScheduledExecutorService 中两种最常用的调度方法 ScheduleAtFixedRate 和 ScheduleWithFixedDelay。
ScheduleAtFixedRate 每次执行时间为上一次任务开始起向后推一个时间间隔,即每次执行时间为 :initialDelay, initialDelay+period, initialDelay+2*period, …;
ScheduleWithFixedDelay 每次执行时间为上一次任务结束起向后推一个时间间隔,即每次执行时间为:initialDelay, initialDelay+executeTime+delay, initialDelay+2*executeTime+2*delay。
由此可见,ScheduleAtFixedRate 是基于固定时间间隔进行任务调度,ScheduleWithFixedDelay 取决于每次任务执行的时间长短,是基于不固定时间间隔进行任务调度。
TimeUnit: 表示给定单元粒度的时间段,它提供在这些单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。TimeUnit 不维护时间信息,但是有助于组织和使用可能跨各种上下文单独维护的时间表示形式。毫微秒定义为千分之一微秒,微秒为千分之一毫秒,毫秒为千分之一秒,一分钟为六十秒,一小时为六十分钟,一天为二十四小时。
才会真正启动一个线程,其余时间 ScheduledExecutor 都是在轮询任务的状态。
实例代码:
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduledExecutorTest implements Runnable {
private String jobName = "";
public ScheduledExecutorTest(String jobName) {
this.jobName = jobName;
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
public void run() {
System.out.println("execute " + jobName);
}
public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors
.newScheduledThreadPool(10);
long initialDelay1 = 1;
long period1 = 1;
// 从现在开始1秒钟之后,每隔1秒钟执行一次job1
scheduledExecutorService.scheduleAtFixedRate(new ScheduledExecutorTest(
"job1"), initialDelay1, period1, TimeUnit.SECONDS);
long initialDelay2 = 1;
long delay2 = 1;
// 从现在开始2秒钟之后,每隔2秒钟执行一次job2
scheduledExecutorService.scheduleWithFixedDelay(
new ScheduledExecutorTest("job2"), initialDelay2, delay2,
TimeUnit.SECONDS);
}
}
运行结果:
execute job1
execute job2
execute job1
execute job2
ScheduledExecutorService 中两种最常用的调度方法 ScheduleAtFixedRate 和 ScheduleWithFixedDelay。
ScheduleAtFixedRate 每次执行时间为上一次任务开始起向后推一个时间间隔,即每次执行时间为 :initialDelay, initialDelay+period, initialDelay+2*period, …;
ScheduleWithFixedDelay 每次执行时间为上一次任务结束起向后推一个时间间隔,即每次执行时间为:initialDelay, initialDelay+executeTime+delay, initialDelay+2*executeTime+2*delay。
由此可见,ScheduleAtFixedRate 是基于固定时间间隔进行任务调度,ScheduleWithFixedDelay 取决于每次任务执行的时间长短,是基于不固定时间间隔进行任务调度。
TimeUnit: 表示给定单元粒度的时间段,它提供在这些单元中进行跨单元转换和执行计时及延迟操作的实用工具方法。TimeUnit 不维护时间信息,但是有助于组织和使用可能跨各种上下文单独维护的时间表示形式。毫微秒定义为千分之一微秒,微秒为千分之一毫秒,毫秒为千分之一秒,一分钟为六十秒,一小时为六十分钟,一天为二十四小时。
相关文章推荐
- Java中基于线程池的任务调度设计
- Java实时多任务调度过程中的安全监控设计
- Java实时多任务调度过程中的安全监控设计
- Java任务调度、线程池、Spring抽象比较
- 推荐一款Java基于最新Spring 4.2.5 集成Quartz 2.2.2 任务调度定时器界面配置版
- Java实时多任务调度过程中的安全监控设计
- 基于生产-消费者模式的任务异步线程池设计与实现
- Java线程池、Spring、任务调度比较
- 深入浅出 Java Concurrency (32): 线程池 part 5 周期性任务调度
- java基于线程池和反射机制实现定时任务完整实例
- JAVA 并发编程-基于线程池设计的ScheduledExecutor(八)
- Java实时多任务调度过程中的安全监控设计
- Java任务调度和线程池理解
- Java实时多任务调度过程中的安全监控设计
- JAVA 并发编程-基于线程池设计的ScheduledExecutor(八)
- java并发多线程-基于线程池设计的ScheduledExecutor任务调度(8)
- 推荐一款Java基于最新Spring 4.2.5 集成Quartz 2.2.2 任务调度定时器界面配置版
- Java实时多任务调度过程中的安全监控设计
- Java实时多任务调度过程中的安全监控设计
- Java实时多任务调度过程中的安全监控设计