ScheduledThreadPoolExecutor类方法理解总结:
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class TestScheduledThreadPoolExecutor {
private static SimpleDateFormat format = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
// ScheduledExecutorService exec=Executors.newScheduledThreadPool(1);
ScheduledThreadPoolExecutor exec = new ScheduledThreadPoolExecutor(1);
/**
* 每隔一段时间打印系统时间,互不影响的<br/> 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期;<br/>
* 也就是将在 initialDelay 后开始执行,然后在initialDelay+period 后执行,<br/> 接着在
* initialDelay + 2 * period 后执行,依此类推。
*/
exec.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out.println(format.format(new Date()));
}
}, 1000, 5000, TimeUnit.MILLISECONDS);
// 开始执行后就触发异常,next周期将不会运行
exec.scheduleAtFixedRate(new Runnable() {
public void run() {
System.out
.println("RuntimeException no catch,next time can't run");
throw new RuntimeException();
}
}, 1000, 5000, TimeUnit.MILLISECONDS);
// 虽然抛出了运行异常,当被拦截了,next周期继续运行
exec.scheduleAtFixedRate(new Runnable() {
public void run() {
try {
throw new RuntimeException();
} catch (Exception e) {
System.out.println("RuntimeException catched,can run next");
}
}
}, 1000, 5000, TimeUnit.MILLISECONDS);
/**
* 创建并执行一个在给定初始延迟后首次启用的定期操作,<br/> 随后,在每一次执行终止和下一次执行开始之间都存在给定的延迟。
*/
exec.scheduleWithFixedDelay(new Runnable() {
public void run() {
System.out.println("scheduleWithFixedDelay:begin,"
+ format.format(new Date()));
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("scheduleWithFixedDelay:end,"
+ format.format(new Date()));
}
}, 1000, 5000, TimeUnit.MILLISECONDS);
/**
* 创建并执行在给定延迟后启用的一次性操作。
*/
exec.schedule(new Runnable() {
public void run() {
System.out.println("The thread can only run once!");
}
}, 5000, TimeUnit.MILLISECONDS);
}
}
分享到:
相关推荐
源码解析文件ScheduledThreadPoolExecutor
java线程类源码Java ScheduledThreadPoolExecutor演示 java.util.concurrent ScheduledThreadPoolExecutor作为java.util.Timer类的现代替代。
NULL 博文链接:https://cywhoyi.iteye.com/blog/1939040
24.Java中atomic包中的原子操作类总结 25.大白话说java并发工具类-CountDownLatch,CyclicBarrier 26.大白话说java并发工具类-Semaphore,Exchanger 27.一篇文章,让你彻底弄懂生产者--消费者问题
主要介绍了java 定时器线程池(ScheduledThreadPoolExecutor),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
今天小编就为大家分享一篇关于Java自带定时任务ScheduledThreadPoolExecutor实现定时器和延时加载功能,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
JDK 1.5提供的ScheduledThreadPoolExecutor执行定时...从ScheduledFutureTask类的定义可以看出,ScheduledFutureTask类是ScheduledThreadPoolExecutor类的私有内部类,继承了FutureTask类,并实现了RunnableScheduledFu
3. “要使用带有ThreadFactory参数的ScheduledThreadPoolExecutor构造方法哦,这样你就可以方便的设置线程名字啦 1. 添加
(所有Class父类)Reflect类(支持C++类反射)Exception类(各类异常)Thread类 (实现了线程的创建,退出)线程池类 (实现了ThreadExecutorPool,ThreadCachedPoolExecutor,ScheduledThreadPoolExecutor)线程锁...
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.runPeriodic(ScheduledThreadPoolExecutor.java:181) at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run...
EJB at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run
Executor框架主要由3部分组成: ...Executor框架有两个关键类实现了ExecutorService接口(ThreadPoolExecutor和ScheduledThreadPoolExecutor)。 异步计算的结果 。包括Future和实现Future的FutureTask类。
AQS相关应用(CountDownLatch、CyclicBarrier、Semaphore等),executor(ThreadPoolExecutor、ScheduledThreadPoolExecutor、FutureTask等),collection(ConcurrentHashMap、CopyOnWriteArrayList等), 对每个类的...
java游戏服务器中,需要使用到的定时器功能 现在要讲解的是一个quartz-all-1.8.6.jar另外一个是ScheduledThreadPoolExecutor。
一个例子是索引更新操作,它的运行时间并不重要,只是它在任何给定时间只在一台服务器上运行。 集成基于 java.util.concurrent.ScheduledThreadPoolExecutor 的自定义实现,因此应该可用于各种应用程序。 用法 ...
dbFramework 简单的数据库存储框架,适合简单的单机游戏服务器/应用服务器使用,省去开发人员自己建表/分表,让开发人员更专注于业务逻辑 ...7.在服务器关闭时需要调用destory方法,此方法会尝试保存所有需要入库但暂时
普通JDK自带的线程池时无法实现线程池的自动切换,基于监控与上下文自动切换的需求,封住了一套taxi开头的线程池,接入方式很简单,它的使用方式与Jdk的使用基本方式一致,只需在对应的类前加一个Taxi,现将对应方式...
Timers schedule one-shot or recurring TimerTask tasks for execution. Prefer java.util.concurrent.ScheduledThreadPoolExecutor ScheduledThreadPoolExecutor for new code.
系统关闭采用线程池中ScheduledThreadPoolExecutor来设置计划任务。 6、用户并发选课控制,采用异步处理,并且使用缓存层Redis记录相关信息,同时采用aop编程思想,在第一次请求选课接口时,加载redis中lua脚本文件...
它的工作方式类似于Java类ScheduledThreadPoolExecutor ,但是它具有一些高级功能: :固定时间(例如00:30),CRON表达式或基于代码的自定义表达式, 可以检索有关每个作业执行的信息, 可能配置, 在较少要同时...