线程池(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 ...
随机推荐
- 多线程mtr-代码
#!/bin/env python # -*- coding: utf-8 -*- # @Date : 2015-09-06 11:30:48 # @Author : Your Name (you@e ...
- BZOJ-1568: Blue Mary开公司 (李超线段树)
Description Input 第一行 :一个整数N ,表示方案和询问的总数. 接下来N行,每行开头一个单词“Query”或“Project”. 若单词为Query,则后接一个整数T,表示Blue ...
- BZOJ_3159_决战
题目链接 分析: 我使用树剖+splay维护这个东西. 对每条重链维护一棵splay,链加和查询正常做,剩下的链反转如下. 由于一定是深度递增的一条链,我们树剖将它分成从左到右log个区间,提取出对应 ...
- CRC16算法之一:CRC16-CCITT-FALSE算法的java实现
CRC16算法系列文章: CRC16算法之一:CRC16-CCITT-FALSE算法的java实现 CRC16算法之二:CRC16-CCITT-XMODEM算法的java实现 CRC16算法之三:CR ...
- poj1734Sightseeing trip——无向图求最小环
题目:http://poj.org/problem?id=1734 无向图求最小环,用floyd: 在每个k点更新f[i][j]之前,以k点作为直接连到i,j组成一个环的点,这样找一下最小环: 注意必 ...
- Mysql常用命令行大全(四)外键及其它
表构成 mysql> show tables; +----------------------+| Tables_in_WebComplie |+----------------------+| ...
- AR/VR-VR-Info-Micron-Insight:一镜观一屋:VR 将建筑设计变为现实
ylbtech-AR/VR-VR-Info-Micron-Insight:一镜观一屋:VR 将建筑设计变为现实 1.返回顶部 1. 一镜观一屋:VR 将建筑设计变为现实 想象一下,在一栋为你设计的还没 ...
- 六 Vue学习 首页 (下)
一:Store介绍: state: 相当于数据 action: action去commit mutations mutation: 只有mutation 才能改变state 例: const stor ...
- caffe 逐步调试
caffe 逐步调试 https://www.zhihu.com/question/27982282
- 关于ajaxfileupload的使用方法以及一些问题
使用问题: 1.ajax-fileupload.js handleError 异常 由于本来handleError方法是jquery的方法,但jquery到了某个版本这个方法就去掉了没有了 所以最简单 ...