Java 线程池
作者:互联网
- 认识cpu、核心与线程
- java多线程系列(一)之java多线程技能
- java多线程系列(二)之对象变量的并发访问
- java多线程系列(三)之等待通知机制
- java多线程系列(四)之ReentrantLock的使用
- java多线程系列(五)之synchronized ReentrantLock volatile Atomic 原理分析
- java多线程系列(六)之线程池原理及其使用
_____________________________________________________________________________
ExecutorService——newScheduledThreadPool使用功能测试
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("======================================");
}
},1000,5000,TimeUnit.MILLISECONDS);
}
}
具体分析
创建一个ScheduledExecutorService线程池的方法,以下为创建一个大小为2的线程池:
ScheduledExecutorService service = Executors.newScheduledThreadPool(2);
- 1
scheduleAtFixedRate
实验一
scheduleAtFixedRate(service,1000);
- 1
输出结果为:
scheduleAtFixedRate 开始执行时间:15:03:15
scheduleAtFixedRate 执行花费时间=1m
scheduleAtFixedRate 执行完成时间:15:03:16
======================================
scheduleAtFixedRate 开始执行时间:15:03:20
scheduleAtFixedRate 执行花费时间=1m
scheduleAtFixedRate 执行完成时间:15:03:21
======================================
分析得出:在任务执行时间小于间隔时间的情况下,程序以起始时间为准则,每隔指定时间执行一次,不受任务执行时间影响。
实验二
scheduleAtFixedRate(service,6000);
- 1
输出结果为:
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
======================================
得出结论:当执行任务大于延迟时间时,第一个任务执行之后,延迟指定时间,然后开始执行第二个任务。
总之:此方法无论任务执行时间长短,都是当第一个任务执行完成之后,延迟指定时间再开始执行第二个任务
_____________________________________________________________________________
Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
_____________________________________________________________________________ java多线程之ForkJoinPool https://blog.csdn.net/tianshi_kco/article/details/53026192 ForkJoinPool 分支/ 合并框架 工作窃取 https://blog.csdn.net/zxm1306192988/article/details/59701101?utm_source=copy [JAVA学习笔记-53]ForkJoin_invoke_submit_execute区别 https://blog.csdn.net/sinat_36263171/article/details/52837867 _____________________________________________________________________________
标签:执行,15,service,时间,scheduleWithFixedDelay,线程,Java,scheduleAtFixedRate 来源: https://www.cnblogs.com/kelelipeng/p/10819044.html