ExecutorService——newScheduledThreadPool使用功能测试
2016-06-01 15:15
656 查看
测试目的
学习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("======================================"); } },1000,5000,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 ======================================
得出结论:当执行任务大于延迟时间时,第一个任务执行之后,延迟指定时间,然后开始执行第二个任务。
总之:此方法无论任务执行时间长短,都是当第一个任务执行完成之后,延迟指定时间再开始执行第二个任务。
相关文章推荐
- 如何利用Watson Analytics进行数据探索
- spring bean初始化
- 怎样在Eclipse中快速查看各种源代码?
- spark 连接hbase
- ExecutorService——newScheduledThreadPool使用功能测试
- heX首页、文档和下载 - 桌面应用软件的跨平台解决方案 - 开源中国社区
- MVVM 模式下iOS项目目录结构详细说明
- UIImageView三种方式 和 位置分布
- Java环境配置问题,Eclipse打不开
- display:flex属性学习
- webapp
- android 软键盘 EditText 使用总结
- Java:重写equals()和hashCode()
- apple pay代码实现
- 友盟第三方推送
- QNX Bootloader 启动顺序
- 小顶堆_优先队列 ,实现哈夫曼树的WPL求值
- XMLHTTP使用详解
- Hibernate配置文件和映射元素解释
- POCO系列之——延迟加载