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的更多相关文章

  1. intellij IDEA 出现“Usage of API documented as @since 1.6+”的解决办法

    问题 在导入java.io.console的时候出现"Usage of API documented as @since 1.6+"

  2. Disk Space Usage 术语理解:unallocated, unused and reserved

    通过standard reports查看Disk Usage,选中Database,右击,选择Reports->Standard Reports->Disk Space Usage,截图如 ...

  3. OpenCascade MeshVS Usage

    OpenCascade MeshVS Usage eryar@163.com Abstract. MeshVS means Mesh Visualization Service. It can be ...

  4. Usage: AddDimensionedImage imageFile outputFile eclipse 运行程序出错

    关于这个在eclipse中运行java程序的错,首先确认你的jdk,jre是否完整,并且与你的eclipse的位数相同,当然我相信这个错误大家应该都会去检查到. 第二个关于addDimensioned ...

  5. Please allow Subclipse team to receive anonymous usage statistics for this Eclipse intance(info)

    本文转载自:http://blog.csdn.net/myfxx/article/details/21096949 今天在用eclipse启动项目的时候发现了一个问题,就是每次启动项目的时候,ecli ...

  6. [转]Dynamic SQL & Stored Procedure Usage in T-SQL

    转自:http://www.sqlusa.com/bestpractices/training/scripts/dynamicsql/ Dynamic SQL & Stored Procedu ...

  7. Java 线程 — ScheduledThreadPoolExecutor

    ScheduledThreadPoolExecutor 该类继承自ThreadPoolExecutor,增加了定时执行线程和延迟启动的功能,这两个功能是通过延时队列DelayedWorkQueue辅助 ...

  8. 处理Linux下subversion尝试连接自建的VisualSVN server报“Key usage violation in certificate has been detected”错误的问题

    在Linux下使用subversion尝试链接VisualSVN server搭建的svn库,可能会报下面错误, svn: OPTIONS of 'https://server.domain.loca ...

  9. 应用alter index ××× monitoring usage;语句监控索引使用与否

    随着时间的累积,在没有很好的规划的情况下,数据库中也许会存在大量长期不被使用的索引,如果快速的定位这些索引以便清理便摆在案头.我们可以使用"alter index ××× monitorin ...

随机推荐

  1. 返回通知 对方法返回的结果可以进行加工 例如请求接口后 返回的json参数可以加工成对象返回给调用者

  2. 一个很变态的SQL

    select max(s.operat_time) as pzTime from ws_state_record s where s.status = (select p1.node_id from ...

  3. 清北澡堂 Day2 下午 一些比较重要的数论知识整理

    1.欧拉定理 设x1,x2,.....,xk,k=φ(n)为1~n中k个与n互质的数 结论一:axi与axj不同余 结论二:gcd(axi,n)=1 结论三:x1,x2,...,xk和ax1,ax2, ...

  4. [洛谷P4234] 最小差值生成树

    题目类型:\(LCT\)动态维护最小生成树 传送门:>Here< 题意:求一棵生成树,其最大边权减最小边权最小 解题思路 和魔法森林非常像.先对所有边进行排序,每次加边的时候删除环上的最小 ...

  5. Docker 错误 docker: invalid reference format. 的解决

    运行 docker run –it –v /dataset:/dataset –v /inference:/inference –v /result:/result floydhub/pytorch: ...

  6. Java【第十篇】集合

    Java 集合概述 Java 集合就像一种容器,可以把多个对象的引用放入容器中.Java 集合类可以用于存储数量不等的多个对象,还可用于保存具有映射关系的关联数组Java 集合可分为 Set.List ...

  7. Time travel HDU - 4418(高斯消元)

    Agent K is one of the greatest agents in a secret organization called Men in Black. Once he needs to ...

  8. 2018年秋季学期《c语言程序设计》编程总结

    <c语言程序设计>第四周编程总结 <c语言程序设计>第五周编程总结 <c语言程序设计>第六周编程总结 <c语言程序设计>第七周编程总结 <c语言程 ...

  9. python三种回收机制

    Python的GC模块主要运用了“引用计数”(reference counting)来跟踪和回收垃圾.在引用计数的基础上,还可以通过“标记-清除”(mark and sweep)解决容器对象可能产生的 ...

  10. saltstack主机管理项目:动态调用插件解析-模块解析(五)

    一.动态调用插件解析 1.目录结构 1.base_module代码解析: def syntax_parser(self,section_name,mod_name,mod_data): print(& ...