ScheduledThreadPoolExecutor Usage
refs:
https://blog.csdn.net/wenzhi20102321/article/details/78681379
对比一下Timer和ScheduledThreadPoolExecutor:
| Timer | ScheduledThreadPoolExecutor |
|---|---|
| 单线程 | 多线程 |
| 单个任务执行时间影响其他任务调度 | 多线程,不会影响 |
| 基于绝对时间 | 基于相对时间 |
| 一旦执行任务出现异常不会捕获,其他任务得不到执行 | 多线程,单个任务的执行不会影响其他线程 |
所以,在JDK1.5之后,应该没什么理由继续使用Timer进行任务调度了。
ScheduledThreadPoolExecutor usage:
ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(2);
scheduled.scheduledAtFixedRate(new Runnable() {
@Override
public void run() {
Log.e("wegeh");
}
}, 0, 40, TimeUnit.MILLISECONDS); // 0表示首次执行任务的延迟时间,40表示每次执行任务的间隔时间,
TimeUnit.MILLISECONDS执行的时间间隔数值单位
ScheduledThreadPoolExecutor的使用
下面用一个具体的例子来说明ScheduledThreadPoolExecutor的使用:
public class ScheduledThreadPoolTest {
public static void main(String[] args) throws InterruptedException {
// 创建大小为5的线程池
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
for (int i = 0; i < 3; i++) {
Task worker = new Task("task-" + i);
// 只执行一次
// scheduledThreadPool.schedule(worker, 5, TimeUnit.SECONDS);
// 周期性执行,每5秒执行一次
scheduledThreadPool.scheduleAtFixedRate(worker, 0,5, TimeUnit.SECONDS);
}
Thread.sleep(10000);
System.out.println("Shutting down executor...");
// 关闭线程池
scheduledThreadPool.shutdown();
boolean isDone;
// 等待线程池终止
do {
isDone = scheduledThreadPool.awaitTermination(1, TimeUnit.DAYS);
System.out.println("awaitTermination...");
} while(!isDone);
System.out.println("Finished all threads");
}
}
class Task implements Runnable {
private String name;
public Task(String name) {
this.name = name;
}
@Override
public void run() {
System.out.println("name = " + name + ", startTime = " + new Date());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("name = " + name + ", endTime = " + new Date());
}
}
from api doc:
public class CustomScheduledExecutor extends ScheduledThreadPoolExecutor {
static class CustomTask<V> implements RunnableSchedluedFuture<V> {...}
protected <V> RunnableSchedluedFuture<V> decorateTask(Runnable r, RunnableSchedluedFuture<V> task) {
return new CustomTask<V> (r, task);
}
protected <V> RunnableSchedluedFuture<V> decorateTask(Callable<V> c, RunnableSchedluedFuture<V task) {
return new CustomTask<V>(c, task);
}
// ... add constructors, etc.
}
ScheduledThreadPoolExecutor Usage的更多相关文章
- intellij IDEA 出现“Usage of API documented as @since 1.6+”的解决办法
问题 在导入java.io.console的时候出现"Usage of API documented as @since 1.6+"
- Disk Space Usage 术语理解:unallocated, unused and reserved
通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...
- OpenCascade MeshVS Usage
OpenCascade MeshVS Usage eryar@163.com Abstract. MeshVS means Mesh Visualization Service. It can be ...
- Usage: AddDimensionedImage imageFile outputFile eclipse 运行程序出错
关于这个在eclipse中运行java程序的错,首先确认你的jdk,jre是否完整,并且与你的eclipse的位数相同,当然我相信这个错误大家应该都会去检查到. 第二个关于addDimensioned ...
- Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)
本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...
- [转]Dynamic SQL & Stored Procedure Usage in T-SQL
转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...
- Java 线程 — ScheduledThreadPoolExecutor
ScheduledThreadPoolExecutor 该类继承自ThreadPoolExecutor,增加了定时执行线程和延迟启动的功能,这两个功能是通过延时队列DelayedWorkQueue辅助 ...
- 处理Linux下subversion尝试连接自建的VisualSVN server报“Key usage violation in certificate has been detected”错误的问题
在Linux下使用subversion尝试链接VisualSVN server搭建的svn库,可能会报下面错误, svn: OPTIONS of 'https://server.domain.loca ...
- 应用alter index ××× monitoring usage;语句监控索引使用与否
随着时间的累积,在没有很好的规划的情况下,数据库中也许会存在大量长期不被使用的索引,如果快速的定位这些索引以便清理便摆在案头.我们可以使用"alter index ××× monitorin ...
随机推荐
- 【XSY3139】预言家 数位DP NFA
题目描述 有一个定义在 \(\{0,1,2,3,4,5,6,7,8,9\}\) 上的合规表达式,包含三种基本的操作: 结合:\(E_1E_2\) 分配:\((E_1|E_2|\ldots|E_n),n ...
- nginx安装访问
依赖包安装: 安装gcc gcc-c++: yum -y install gcc gcc-c++ autoconf automake 安装pcre: yum -y install pcre pcre- ...
- jdbc增删改查进行封装
jdbc封装 1 dao (代码分层) com.aaa.dao 存放dao相关的类型 例如 StudentDAOImpl 处理 数据库的链接 存取数据 com.aaa.servlet 存放servle ...
- 主机管理+堡垒机系统开发:webssh(十)
一.安装shellinabox 1.安装依赖工具 yum install git openssl-devel pam-devel zlib-devel autoconf automake libtoo ...
- ZooKeeper-集群模式安装
下载地址:https://zookeeper.apache.org/releases.html 至少需要准备三台节点(这里为h136.h138.h140),ZooKeeper 需要 JDK,关于 JD ...
- WEB内容换行
word-wrap:break-word 单词间换行 word-break:break-all 单词内也可以换行 white-space属性指定元素内的空白怎样处理 normal 默认.空白会被浏览器 ...
- Best Practice API
# 建议直接使用的第三方类 Common Lang =>StringUtils =>Validate Guava =>Cache =>Ordering JDK7(LTS JDK ...
- java8 按对象属性值分组
Map<String, List<User>> userMap = list.stream().collect(Collectors.groupingBy(User::getG ...
- C++ 在类里面使用多线程技术
参考链接: https://blog.csdn.net/jmh1996/article/details/72235232 说明: C++的每个成员函数都会有一定转化, 而static 则不会 ...
- vue封装axios方法推荐)
目录结构: api.js export default { myTopic: '/api/subscribe-data/post/cat' } request.js import axios from ...