https://blog.csdn.net/wo541075754/article/details/51556198css
学习ScheduledExecutorService类建立的newScheduledThreadPool相关用法html
建立newScheduledThreadPool及scheduleAtFixedRate和scheduleWithFixedDelay方法的使用。java
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的线程池:markdown
输出结果为:多线程
scheduleAtFixedRate 开始执行时间:15:03:15
scheduleAtFixedRate 执行花费时间=1m
scheduleAtFixedRate 执行完成时间:15:03:16 ====================================== scheduleAtFixedRate 开始执行时间:15:03:20 scheduleAtFixedRate 执行花费时间=1m scheduleAtFixedRate 执行完成时间:15:03:21 ======================================
分析得出:在任务执行时间小于间隔时间的状况下,程序以起始时间为准则,每隔指定时间执行一次,不受任务执行时间影响。并发
输出结果为:框架
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
分析得出:当执行任务时间大于间隔时间,此方法不会从新开启一个新的任务进行执行,而是等待原有任务执行完成,立刻开启下一个任务进行执行。此时,执行间隔时间已经被打乱。ide
scheduleWithFixedDelay(service,1000);
输出结果为:post
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 ======================================
得出结论:当执行任务大于延迟时间时,第一个任务执行以后,延迟指定时间,而后开始执行第二个任务。
总之:此方法不管任务执行时间长短,都是当第一个任务执行完成以后,延迟指定时间再开始执行第二个任务