public class ScheduledThreadPoolExecutor
extends ThreadPoolExecutor
implements ScheduledExecutorService {

public interface ScheduledExecutorService extends ExecutorService {

ScheduledExecutorService多了四个延时或周期任务接口

//达到给定的延时时间后,执行任务。这里传入的是实现Runnable接口的任务,
//因此通过ScheduledFuture.get()获取结果为null
public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit);
//达到给定的延时时间后,执行任务。这里传入的是实现Callable接口的任务,
//因此,返回的是任务的最终计算结果public <V> ScheduledFuture<V> schedule(Callable<V> callable,long delay, TimeUnit unit);//是以上一个任务开始的时间计时,period时间过去后,
//检测上一个任务是否执行完毕,如果上一个任务执行完毕,
//则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);
//当达到延时时间initialDelay后,任务开始执行。上一个任务执行结束后到下一次
//任务执行,中间延时时间间隔为delay。以这种方式,周期性执行任务。
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit);

使用

    public static void main(String[] args) throws ExecutionException, InterruptedException {ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);System.out.println("main " + new Date());ScheduledFuture<?> schedule = pool.schedule(new Runnable() {@Overridepublic void run() {System.out.println("schedule Runnable " + new Date());}}, 1, TimeUnit.SECONDS);System.out.println(schedule.get());ScheduledFuture<Integer> schedule1 = pool.schedule(new Callable<Integer>() {@Overridepublic Integer call() throws Exception {System.out.println("schedule Callable " + new Date());return 1;}}, 2, TimeUnit.SECONDS);System.out.println(schedule1.get());}

main Wed Sep 04 11:07:42 CST 2019
schedule Runnable Wed Sep 04 11:07:43 CST 2019
null
schedule Callable Wed Sep 04 11:07:45 CST 2019
1

scheduleAtFixedRate使用

延时1秒后,执行第一次任务,周期2秒过后,再次执行任务

    public static void main(String[] args) throws ExecutionException, InterruptedException {ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);System.out.println("main " + new Date());ScheduledFuture<?> schedule = pool.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {System.out.println("schedule Runnable " + new Date());}}, 1, 2, TimeUnit.SECONDS);System.out.println(schedule.get());}

main Wed Sep 04 11:14:06 CST 2019
schedule Runnable Wed Sep 04 11:14:07 CST 2019
schedule Runnable Wed Sep 04 11:14:09 CST 2019
schedule Runnable Wed Sep 04 11:14:11 CST 2019

如果任务时间3秒,大于周期时间2秒,等待任务执行完毕后,立即再次执行任务

    public static void main(String[] args) throws ExecutionException, InterruptedException {ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);System.out.println("main " + new Date());ScheduledFuture<?> schedule = pool.scheduleAtFixedRate(new Runnable() {@Overridepublic void run() {System.out.println("schedule Runnable " + new Date());try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}}}, 1, 2, TimeUnit.SECONDS);System.out.println(schedule.get());}

main Wed Sep 04 11:16:12 CST 2019
schedule Runnable Wed Sep 04 11:16:13 CST 2019
schedule Runnable Wed Sep 04 11:16:16 CST 2019
schedule Runnable Wed Sep 04 11:16:19 CST 2019

scheduleWithFixedDelay使用

等待任务执行完毕后,延时2秒,再次执行任务

   public static void main(String[] args) throws ExecutionException, InterruptedException {ScheduledExecutorService pool = Executors.newScheduledThreadPool(4);System.out.println("main " + new Date());ScheduledFuture<?> schedule = pool.scheduleWithFixedDelay(new Runnable() {@Overridepublic void run() {System.out.println("schedule Runnable " + new Date());try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}}}, 1, 2, TimeUnit.SECONDS);System.out.println(schedule.get());}

main Wed Sep 04 11:18:11 CST 2019
schedule Runnable Wed Sep 04 11:18:12 CST 2019
schedule Runnable Wed Sep 04 11:18:17 CST 2019
schedule Runnable Wed Sep 04 11:18:22 CST 2019

ScheduledThreadPoolExecutor相关推荐

  1. ScheduledThreadPoolExecutor()定时执行线程池详解,java线程池

    为什么80%的码农都做不了架构师?>>>    package com.dy.pool;import java.util.concurrent.ExecutorService; im ...

  2. ScheduledThreadPoolExecutor详解

    2019独角兽企业重金招聘Python工程师标准>>> 本文主要分为两个部分,第一部分首先会对ScheduledThreadPoolExecutor进行简单的介绍,并且会介绍其主要A ...

  3. ScheduledThreadPoolExecutor的相关知识

    转载请标明出处:[顾林海的博客] 个人开发的微信小程序,目前功能是书籍推荐,后续会完善一些新功能,希望大家多多支持! ##前言 ScheduledThreadPoolExecutor继承自Thread ...

  4. (二十)java多线程之ScheduledThreadPoolExecutor

    本人邮箱: <kco1989@qq.com> 欢迎转载,转载请注明网址 http://blog.csdn.net/tianshi_kco github: https://github.co ...

  5. java定时器返回future_java 定时器线程池(ScheduledThreadPoolExecutor)的实现

    前言 定时器线程池提供了定时执行任务的能力,即可以延迟执行,可以周期性执行.但定时器线程池也还是线程池,最底层实现还是ThreadPoolExecutor,可以参考我的另外一篇文章多线程–精通Thre ...

  6. ScheduledThreadPoolExecutor定时任务线程池执行原理分析

    一.示例代码 @Slf4j public class ScheduleThreadPoolTest {private static ScheduledExecutorService executor ...

  7. 使用ScheduledThreadPoolExecutor代替TimerTimerTask

    如果我们想要延迟(deferred)或者周期性(periodic)执行一个任务,我们可以使用Java API 的Timer和TimerTask类. 一般步骤是: 继承TimerTask(抽象类),复写 ...

  8. 线程池之ScheduledThreadPoolExecutor线程池源码分析笔记

    1.ScheduledThreadPoolExecutor 整体结构剖析. 1.1类图介绍 根据上面类图图可以看到Executor其实是一个工具类,里面提供了好多静态方法,根据用户选择返回不同的线程池 ...

  9. Java并发编程—ScheduledThreadPoolExecutor原理分析

    原文作者:小付 原文地址:ScheduledThreadPoolExecutor原理分析 目录 一.简单使用 二.类UML图 三.处理流程 四.任务提交方式 五.SchduledFutureTask之 ...

  10. java定时线程池_java 定时器线程池(ScheduledThreadPoolExecutor)的实现

    前言 定时器线程池提供了定时执行任务的能力,即可以延迟执行,可以周期性执行.但定时器线程池也还是线程池,最底层实现还是ThreadPoolExecutor,可以参考我的另外一篇文章多线程–精通Thre ...

最新文章

  1. TensorRT 基于Yolov3的开发
  2. 线程池之CachedThreadPool学习
  3. slimftp超小型的FTP服务器
  4. div方框弯曲边样式_使用弯曲样式编辑文本
  5. pomelo获取客户端IP
  6. 数据结构与算法 汉诺塔问题和列车车厢重排问题
  7. 微信用户全局唯一标识_忘掉 Snowflake,感受一下性能高出587倍的全局唯一ID生成算法...
  8. node.js 事件循环
  9. 关于文件上传,我要向struts提点意见
  10. 《how to write and publis a scientific paper》 Chapter 3
  11. HTML代码页面无法跳转为什么,html跳转新页面代码_html页面跳转代码
  12. (源码)在LibVLC中增加录制接口libvlc_media_player_recorder_start
  13. 开源中国正式挂牌,新三板首家软件众包平台
  14. 使用GLAD加载OpenGL的库
  15. 上位机PC控制UR3机器人实现方式
  16. 搭建hexo个人网站小试
  17. linux cp指令报错:cp: -r not specified; cp: omitting directory ‘xxx‘(需要加-r递归拷贝)
  18. U盘偷资料神器,我都不太敢分享了。
  19. iOS中 Realm的学习与使用 韩俊强的博客
  20. sidebar(侧边栏文字)

热门文章

  1. 零基础带你学习MySQL—创建表(四)
  2. jQuery学习(五)—课堂实训题专栏
  3. JavaScript学习(十四)—元素节点关系和特殊节点
  4. 难道早上起床后就一定要喝一杯白开水吗?
  5. 人穷的时候,最看不起自己的是谁?
  6. 螃蟹为什么横着走,今天为大家介绍为什么螃蟹横着走
  7. 玩抖音,你喜欢的,都是对自身没好处的
  8. 有人羡慕过自由职业吗?
  9. 面向对象编程(Object-Oriented Programming)
  10. 浏览网页时,手机显示手机被恶意攻击,不停震动,一直弹出应用要我下载,有没有问题?