前端开发 \ JavaScript \ ExecutorService——newScheduledThreadPool使用功能测试

ExecutorService——newScheduledThreadPool使用功能测试

总点击59
简介:版权声明:本文为博主原创文章,转载请写明出处。https://blog.csdn.net/wo541075754/article/details/51556198

版权声明:本文为博主原创文章,转载请写明出处。 https://blog.csdn.net/wo541075754/article/details/51556198

测试目的

学习ScheduledExecutorService类创建的newScheduledThreadPool相关用法

具体事例

创建newScheduledThreadPool及scheduleAtFixedRate和scheduleWithFixedDelay方法的使用。

package com.secbro.test.thread;

import java.text.DateFormat;

import java.util.Date;

import java.util.concurrent.Executors;

import java.util.concurrent.ScheduledExecutorService;

import java.util.concurrent.TimeUnit;

/**

* 创建一个大小无限的线程池。此线程池支持定时以及周期性执行任务的需求。

* @author zhuzhisheng

* @Description

* @date on 2016/6/1.

*/

public class TestNewScheduledThreadPool {

public static void main(String[] args) {

ScheduledExecutorService service = Executors.newScheduledThreadPool(2);

scheduleAtFixedRate(service,1000);

scheduleAtFixedRate(service,6000);

scheduleWithFixedDelay(service,1000);

scheduleWithFixedDelay(service,6000);

}

private static void scheduleAtFixedRate(ScheduledExecutorService service,final int sleepTime){

service.scheduleAtFixedRate(new Runnable() {

@Override

public void run() {

long start = new Date().getTime();

System.out.println("scheduleAtFixedRate 开始执行时间:" +

DateFormat.getTimeInstance().format(new Date()));

try {

Thread.sleep(sleepTime);

} catch (InterruptedException e) {

e.printStackTrace();

}

long end = new Date().getTime();

System.out.println("scheduleAtFixedRate 执行花费时间=" + (end -start)/1000 + "m");

System.out.println("scheduleAtFixedRate 执行完成时间:"

+ DateFormat.getTimeInstance().format(new Date()));

System.out.println("======================================");

}

},1000,5000,TimeUnit.MILLISECONDS);

}

private static void scheduleWithFixedDelay(ScheduledExecutorService service,final int sleepTime){

service.scheduleWithFixedDelay(new Runnable() {

@Override

public void run() {

long start = new Date().getTime();

System.out.println("scheduleWithFixedDelay 开始执行时间:" +

DateFormat.getTimeInstance().format(new Date()));

try {

Thread.sleep(sleepTime);

} catch (InterruptedException e) {

e.printStackTrace();

}

long end = new Date().getTime();

System.out.println("scheduleWithFixedDelay执行花费时间=" + (end -start)/1000 + "m");

System.out.println("scheduleWithFixedDelay执行完成时间:"

+ DateFormat.getTimeInstance().format(new Date()));

System.out.println("======================================");

}

},TimeUnit.MILLISECONDS);

}

}

具体分析

创建一个ScheduledExecutorService线程池的方法,以下为创建一个大小为2的线程池:

ScheduledExecutorService service = Executors.newScheduledThreadPool(2);

scheduleAtFixedRate

实验一

scheduleAtFixedRate(service,1000);

输出结果为:

scheduleAtFixedRate 开始执行时间:15:03:15

scheduleAtFixedRate 执行花费时间=1m

scheduleAtFixedRate 执行完成时间:15:03:16

======================================

scheduleAtFixedRate 开始执行时间:15:03:20

scheduleAtFixedRate 执行花费时间=1m

scheduleAtFixedRate 执行完成时间:15:03:21

======================================

分析得出:在任务执行时间小于间隔时间的情况下,程序以起始时间为准则,每隔指定时间执行一次,不受任务执行时间影响。

实验二

scheduleAtFixedRate(service,6000);

输出结果为:

scheduleAtFixedRate 开始执行时间:15:06:12

scheduleAtFixedRate 执行花费时间=6m

scheduleAtFixedRate 执行完成时间:15:06:18

======================================

scheduleAtFixedRate 开始执行时间:15:06:18

scheduleAtFixedRate 执行花费时间=6m

scheduleAtFixedRate 执行完成时间:15:06:24

======================================

scheduleAtFixedRate 开始执行时间:15:06:24

scheduleAtFixedRate 执行花费时间=6m

scheduleAtFixedRate 执行完成时间:15:06:30

分析得出:当执行任务时间大于间隔时间,此方法不会重新开启一个新的任务进行执行,而是等待原有任务执行完成,马上开启下一个任务进行执行。此时,执行间隔时间已经被打乱。

scheduleWithFixedDelay

实验一

scheduleWithFixedDelay(service,1000);

输出结果为:

scheduleWithFixedDelay 开始执行时间:15:11:03

scheduleWithFixedDelay执行花费时间=1m

scheduleWithFixedDelay执行完成时间:15:11:04

======================================

scheduleWithFixedDelay 开始执行时间:15:11:09

scheduleWithFixedDelay执行花费时间=1m

scheduleWithFixedDelay执行完成时间:15:11:10

======================================

分析得出:当执行任务小于延迟时间时,第一个任务执行之后,延迟指定时间,然后开始执行第二个任务。

实验二

scheduleWithFixedDelay(service,6000);

输出结果为:

scheduleWithFixedDelay 开始执行时间:15:12:53

scheduleWithFixedDelay执行花费时间=6m

scheduleWithFixedDelay执行完成时间:15:12:59

======================================

scheduleWithFixedDelay 开始执行时间:15:13:04

scheduleWithFixedDelay执行花费时间=6m

scheduleWithFixedDelay执行完成时间:15:13:10

======================================

得出结论:当执行任务大于延迟时间时,第一个任务执行之后,延迟指定时间,然后开始执行第二个任务。

总之:此方法无论任务执行时间长短,都是当第一个任务执行完成之后,延迟指定时间再开始执行第二个任务。

意见反馈 常见问题 官方微信 返回顶部