线程池(5)Executors.newScheduledThreadPool
例子1(scheduleAtFixedRate):延迟2秒后,每隔3秒执行1次
ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
log.info("开始时间");
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
}
};
es.scheduleAtFixedRate(syncRunnable, 2000, 3000, TimeUnit.MILLISECONDS);
运行结果:
10:49:22.495 开始时间
10:49:24.500 pool-1-thread-1
10:49:27.499 pool-1-thread-1
10:49:30.500 pool-1-thread-2
10:49:33.500 pool-1-thread-1
10:49:36.501 pool-1-thread-3
10:49:39.500 pool-1-thread-2
10:49:42.500 pool-1-thread-2
10:49:45.500 pool-1-thread-1
10:49:48.501 pool-1-thread-1
10:49:51.501 pool-1-thread-1
10:49:54.501 pool-1-thread-4
10:49:57.501 pool-1-thread-2
10:50:00.502 pool-1-thread-2
10:50:03.502 pool-1-thread-3
10:50:06.502 pool-1-thread-3
10:50:09.502 pool-1-thread-3
10:50:12.503 pool-1-thread-5
10:50:15.503 pool-1-thread-5
10:50:18.503 pool-1-thread-5
10:50:21.503 pool-1-thread-5
10:50:24.503 pool-1-thread-3
10:50:27.503 pool-1-thread-2
10:50:30.503 pool-1-thread-1
10:50:33.504 pool-1-thread-4
10:50:36.504 pool-1-thread-5
10:50:39.504 pool-1-thread-5
10:50:42.504 pool-1-thread-2
例子2(scheduleWithFixedDelay):延迟5秒后,每个任务执行完后延迟3秒在执行1次
ScheduledExecutorService es = Executors.newScheduledThreadPool(5);
log.info("开始时间");
Runnable syncRunnable = new Runnable() {
@Override
public void run() {
log.info(Thread.currentThread().getName());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
es.scheduleWithFixedDelay(syncRunnable, 5000, 3000, TimeUnit.MILLISECONDS);
运行结果:
11:10:27.773 开始时间
11:10:32.778 pool-1-thread-1
11:10:36.779 pool-1-thread-1
11:10:40.780 pool-1-thread-2
11:10:44.781 pool-1-thread-1
11:10:48.783 pool-1-thread-3
11:10:52.785 pool-1-thread-3
11:10:56.785 pool-1-thread-4
11:11:00.787 pool-1-thread-4
11:11:04.788 pool-1-thread-4
11:11:08.789 pool-1-thread-4
11:11:12.790 pool-1-thread-3
11:11:16.792 pool-1-thread-1
本来是每隔3秒执行的,但是,由于某个任务处理时间过长,导致延后。本例是延后1秒,即4秒。
总结:scheduleAtFixedRate与scheduleWithFixedDelay区别
scheduleAtFixedRate:不管任务是否执行完了,在3秒内必须执行
scheduleWithFixedDelay:等任务执行完了,在等3秒后执行
因此,scheduleWithFixedDelay 非常有用。
线程池(5)Executors.newScheduledThreadPool的更多相关文章
- 线程池之 Executors
线程池之 Executors + 面试题 线程池的创建分为两种方式:ThreadPoolExecutor 和 Executors,上一节学习了 ThreadPoolExecutor 的使用方式,本节重 ...
- Java 四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
介绍new Thread的弊端及Java四种线程池的使用,对Android同样适用.本文是基础篇,后面会分享下线程池一些高级功能. 1.new Thread的弊端执行一个异步任务你还只是如下new T ...
- Java四种线程池newCachedThreadPool,newFixedThreadPool,newScheduledThreadPool,newSingleThreadExecutor
1.new Thread的弊端 执行一个异步任务你还只是如下new Thread吗? Java new Thread(new Runnable() { @Override public void ru ...
- 线程池工厂Executors编程的艺术
Executors是一个线程池的工厂类,提供各种有用的线程池的创建,使用得当,将会使我们并发编程变得简单!今天就来聊聊这个工厂类的艺术吧! Executors只是Executor框架的主要成员组件之一 ...
- Java线程池ThreadPoolExecutor&&Executors
一.先看看传统的开启线程. new Thread(new Runnable() { @Override public void run() { } }).start(); 缺点: 1.每次new Th ...
- Java并发包线程池之Executors、ExecutorCompletionService工具类
前言 前面介绍了Java并发包提供的三种线程池,它们用处各不相同,接下来介绍一些工具类,对这三种线程池的使用. Executors Executors是JDK1.5就开始存在是一个线程池工具类,它定义 ...
- 线程池(2)-Executors提供4个线程池
1.为什么不使用Executors提供4个线程池创建线程池 阿里巴巴开放手册这样写: . [强制]线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式, ...
- 线程池工厂方法newScheduledThreadPool(),计划任务
package com.thread.test.ThreadPool; import java.util.concurrent.Executors; import java.util.concurre ...
- Android线程池使用终结版
有一段时间没写博文了,今天抽空总结一下,也希望能通过自己写的这些文章,加深理解的同时能帮 助在技术方面有疑点的朋友搞清楚个所以然来,由于经常会在网上或群里看到有朋友会问线程方面的 东西,就像我一个朋友 ...
- Java-五种线程池,四种拒绝策略,三种阻塞队列(转)
Java-五种线程池,四种拒绝策略,三种阻塞队列 三种阻塞队列: BlockingQueue<Runnable> workQueue = null; workQueue = n ...
随机推荐
- Workerman安装流程
第一步检测安装环境 curl -Ss http://www.workerman.net/check.php | php 操作结果显示 报错了 需要找到php.ini文件 解决办法如下: 打开 php ...
- create-react-app使用的问题
// 设置 npm config set registry https://registry.npm.taobao.org // 验证是否成功 npm config get registry或npm ...
- Laravel的三种安装方法总结
Laravel号称巨匠级PHP框架,越来越多的PHPer选择它作为开发框架,作为一个Laravel初学者相信很多人向我一样被安装挡在了门外.所以今天结合文档和自己的学习经历总结一下Laravel的安装 ...
- CodeForces - 204C Little Elephant and Furik and Rubik
CodeForces - 204C Little Elephant and Furik and Rubik 个人感觉是很好的一道题 这道题乍一看我们无从下手,那我们就先想想怎么打暴力 暴力还不简单?枚 ...
- 1068 Bash游戏 V3
1068 Bash游戏 V3 题目来源: Ural 1180 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 收藏 关注 有一堆石子共有N个.A B两个人轮流拿 ...
- BZOJ3674:可持久化并查集加强版
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 题目传送门:https://www.lydsy.com/JudgeOnline/problem.p ...
- Mysql MMM 高可用
一.Mysql MMM 高可用概况: mmm_mond 负责所有的监控工作的监控守护进程,决定节点的移除等: mmm_agentd 运行在mysql服务器上的代理守护进程,通过简单远程服务集提供给 ...
- sublime text3设置
我的sublime的设置,ps:这个博文只是为了我自己的一个记录 { "color_scheme": "Packages/Theme - Solarized Flat/S ...
- 二维数组是二级指针pointer to pointer!
二维数组居然是个类似于二级指针(pointer to pointer)的东西,十分震惊! #include <stdio.h> int main() { ][]={{,,,},{,,,}, ...
- javascript 函数属性prototype(转)
在JavaScript中并没有类的概念,但javascript中的确可以实现重载,多态,继承.这些实现其实方法都可以用JavaScript中的引用和变量作用域结合prototype来解释. 1.pro ...