例子1:延迟3秒后,只执行1次

ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
log.info("开始时间");
try {
for (int i = 0; i < 20; i++) {
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
}
};
es.schedule(syncRunnable, 3000, TimeUnit.MILLISECONDS);
}
} finally {
es.shutdown();
}

运行结果:

            10:41:39.589 开始时间
10:41:42.595 pool-1-thread-1
10:41:42.595 pool-1-thread-2
10:41:42.595 pool-1-thread-3
10:41:42.595 pool-1-thread-4
10:41:42.596 pool-1-thread-2
10:41:42.596 pool-1-thread-5
10:41:42.596 pool-1-thread-1
10:41:42.596 pool-1-thread-3
10:41:42.597 pool-1-thread-4
10:41:42.597 pool-1-thread-2
10:41:42.597 pool-1-thread-5
10:41:42.597 pool-1-thread-1
10:41:42.598 pool-1-thread-3
10:41:42.598 pool-1-thread-4
10:41:42.598 pool-1-thread-2
10:41:42.599 pool-1-thread-5
10:41:42.599 pool-1-thread-1
10:41:42.600 pool-1-thread-3
10:41:42.600 pool-1-thread-4
10:41:42.600 pool-1-thread-2

调用的ThreadPoolExecutor:

public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,
new DelayedWorkQueue());
}

corePoolSize=5,maximumPoolSize=Integer.MAX_VALUE

keepAliveTime=0纳秒

allowCoreThreadTimeout=false(默认)

采用延迟队列DelayedWorkQueue

因此,

  • 线程池中的线程数永远是5,永久存活
  • 对于新任务,当队列未满时,插入队列;当队列已满时,默认执行AbortPolicy,即抛出异常。
  • 支持线程reuse

因此,类似newFixedThreadPool

public ScheduledThreadPoolExecutor(int corePoolSize) {        super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,              new DelayedWorkQueue());    }

线程池(4)Executors.newScheduledThreadPool-只执行1次的更多相关文章

  1. Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor

    介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...

  2. Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor

    1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? Java new Thread(new Runnable() { @Override public void ru ...

  3. 线程池工厂Executors编程的艺术

    Executors是一个线程池的工厂类,提供各种有用的线程池的创建,使用得当,将会使我们并发编程变得简单!今天就来聊聊这个工厂类的艺术吧! Executors只是Executor框架的主要成员组件之一 ...

  4. Java并发包线程池之Executors、ExecutorCompletionService工具类

    前言 前面介绍了Java并发包提供的三种线程池,它们用处各不相同,接下来介绍一些工具类,对这三种线程池的使用. Executors Executors是JDK1.5就开始存在是一个线程池工具类,它定义 ...

  5. 线程池之 Executors

    线程池之 Executors + 面试题 线程池的创建分为两种方式:ThreadPoolExecutor 和 Executors,上一节学习了 ThreadPoolExecutor 的使用方式,本节重 ...

  6. ScheduledThreadPoolExecutor源码分析-你知道定时线程池是如何实现延迟执行和周期执行的吗?

    Java版本:8u261. 1 简介 ScheduledThreadPoolExecutor即定时线程池,是用来执行延迟任务或周期性任务的.相比于Timer的单线程,定时线程池在遇到任务抛出异常的时候 ...

  7. Java线程池ThreadPoolExecutor&&Executors

    一.先看看传统的开启线程. new Thread(new Runnable() { @Override public void run() { } }).start(); 缺点: 1.每次new Th ...

  8. 线程池工厂方法newScheduledThreadPool(),计划任务

    package com.thread.test.ThreadPool; import java.util.concurrent.Executors; import java.util.concurre ...

  9. 线程池(2)-Executors提供4个线程池

    1.为什么不使用Executors提供4个线程池创建线程池 阿里巴巴开放手册这样写: . [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式, ...

  10. 怎么判断ThreadPool线程池里的任务都执行完毕

    在下面 链接中做方便的应该是第三种方法(他也推荐了),但是第三种方法有个小问题,就是 : WaitHandle.WaitAll(_ManualEvents); 中的_ManualEvents最大为64 ...

随机推荐

  1. T58

    他们投产的新轿车广获传媒报道.The launch of their new saloon received much media coverage.法律规定禁止近亲结婚.Marriages in p ...

  2. 「LOJ#10042」「一本通 2.1 练习 8」收集雪花 (map

    题目描述 不同的雪花往往有不同的形状.在北方的同学想将雪花收集起来,作为礼物送给在南方的同学们.一共有 n 个时刻,给出每个时刻下落雪花的形状,用不同的整数表示不同的形状.在收集的过程中,同学们不希望 ...

  3. CF285 E Positions in Permutations——“恰好->大于”的容斥和允许“随意放”的dp

    题目:http://codeforces.com/contest/285/problem/E 是2018.7.31的一场考试的题,当时没做出来. 题解:http://www.cnblogs.com/y ...

  4. Zigbee协议栈--Z-Stack的使用

    使用方法简介:一般情况下用户只需要额外添加三个文件就可以完成一个项目.一个是主文件,存放具体的任务事件处理函数:一个是这个主文件的头文件:另外一个是以Osal开头的操作系统接口文件,是专门存放任务处理 ...

  5. python set集合的用法

    python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...

  6. 面向对象(static关键字)

    static关键字:用于修饰成员(成员变量和成员函数) 被修饰后的成员具备以下特点: 随着类的加载而加载 优先于对象存在 被所有的对象共享 可以直接被类名调用 使用注意: 静态方法只能访问静态成员 静 ...

  7. MySQL 的索引和最左前缀原则

    这两天看<构建高性能Web站点>这本书,感觉写的真是不错,很多实际项目中会碰到的问题都有所提及,今天看到一个最左前缀原则,以前也听说过,不过一直没搞明白,今天查了下. 通过实例理解单列索引 ...

  8. QListWidget笔记

    1.头文件:#include <QListWidget> 2.继承自:QListView 3.基本代码: #include "mainwindow.h" #includ ...

  9. 极客时间_Vue开发实战_汇总贴

    视频地址: https://time.geekbang.org/course/intro/163 https://github.com/tangjinzhou/geektime-vue-1 电脑dem ...

  10. 4.ctf实战题

    一道ctf实战题. 先亮出网址: http://fb2ad00f-0a28-4e38-8fff-849d7391e2d0.coding.io 打开连接,看到下面页面.Web题,首先就是扫描(御剑啊还有 ...