public interface ScheduledExecutorService extends ExecutorService {
// 创建在指定延迟后执行且只运行一次的的任务
public ScheduledFuture<?> schedule(Runnable command,
long delay, TimeUnit unit);
// 同上,这里是callable对象
public <V> ScheduledFuture<V> schedule(Callable<V> callable,
long delay, TimeUnit unit);
// 创建在初始延迟后执行并周期性调用的的任务
// 如果执行任务时间大于周期,则下一个任务开始时间会推迟,不会存在并行执行。
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
// 创建在初始延迟后执行并周期性调用的的任务。
// 和上面的区别:这里的delay是前一个任务执行完成后,和下一个任务开始时间的间隔。
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
}
测试:
public class ScheduledExecutorServiceTest {
private static final int POOL_SIZE = 4;
private static ScheduledExecutorService ses = null;
@org.junit.Before
public void setUp() {
ses = Executors.newScheduledThreadPool(POOL_SIZE);
}
@After
public void tearDown() {
ses.shutdown();
}
@Test
public void scheduled() throws ExecutionException, InterruptedException {
MyCall task = new MyCall();
System.out.println(System.currentTimeMillis() + " : scheduled...");
// 延时100+1000毫秒打印
ScheduledFuture<String> future = ses.schedule(task, 100, TimeUnit.MILLISECONDS);
String result = future.get();
System.out.println(System.currentTimeMillis() + " result: " + result);
}
@Test
public void scheduledAtFixedRateTest() throws InterruptedException {
MyCmd myCmd = new MyCmd();
System.out.println(System.currentTimeMillis() + " : scheduledAtFixedRate...");
// 首次延迟100毫秒,然后每隔max(2000,3000)毫秒打印
ses.scheduleAtFixedRate(myCmd, 100, 2000, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(60000);
}
@Test
public void scheduledAfFixedDelayTest() throws InterruptedException {
MyCmd myCmd = new MyCmd();
System.out.println(System.currentTimeMillis() + " : scheduledAfFixedDelay...");
// 首次延迟100毫秒,然后每隔5000毫秒打印
ses.scheduleWithFixedDelay(myCmd, 100, 2000, TimeUnit.MILLISECONDS);
TimeUnit.MILLISECONDS.sleep(60000);
}
static class MyCall implements Callable<String> {
@Override
public String call() throws Exception {
TimeUnit.MILLISECONDS.sleep(1000);
return "MyTask is done";
}
}
static class MyCmd implements Runnable {
@Override
public void run() {
try {
TimeUnit.MILLISECONDS.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(System.currentTimeMillis() + " : MyCmd is done!");
}
}
}
scheduled输出 (延时100+1000毫秒打印)
1505538732156 : scheduled...
1505538733257 result : MyTask is done
scheduledAtFixedRateTest输出 (间隔3000毫秒=max(2000, 3000)):
1505538684893 :scheduledAtFixedRate...
1505538687996 : MyCmd is done!
1505538690997 : MyCmd is done!
scheduledAtFixedDelayTest输出 (间隔5000毫秒=2000+3000):
1505536678015 :scheduledAfFixedDelay...
1505536681117 : MyCmd is done!
1505536686118 : MyCmd is done!
分享到:
相关推荐
定时器小例子,这是一个经较好定时器应用
这是一个jar,很秀。自定义的一个Java定时器工具类。主要是结合Spring Boot一起使用,并在Spring Boot启动的时候一起启动运行。
ScheduledExecutorService 计时器任务处理,用于超时处理。
主要介绍了ScheduledExecutorService任务定时代码示例,具有一定借鉴价值,需要的朋友可以参考下
您可以在工作线程上运行任务(Runnable或Callable),以指定执行计划(如ScheduledExecutorService),并指定执行时间。 介面 ScheduledFuture<?> schedule( Runnable command, int executeTime, long delay,...
下面小编就为大家带来一篇基于ScheduledExecutorService的两种方法(详解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
Springmvc java注解设置定时任务实例,利用 ScheduledExecutorService 接口实现
Java通用任务执行程序,基于java.util.concurrent.ScheduledExecutorService。 Java通用的任务执行器,基于java.util.concurrent.ScheduledExecutorService工具类实现。 Maven: <groupId>...
springMVC配置、注解、列子
ScheduledExecutorService 的测试实用程序实现 允许测试如下代码: class Foo { private int count = 0; public Foo(ScheduledExecutorService service, Bar bar) { service.scheduleAtFixedRate(new Runnable...
调度可以用 Timer 【调用 schedule() 或者 scheduleAtFixedRate() 方法实现】或者 ScheduledExecutorService 【结合工作中其它的需求,笔者选用此】 ScheduledExecutorService的初始化(线程池):
private ScheduledExecutorService scheduledExecutorService; //切换到当前显示的图片 private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { viewPager....
1.普通thread实现 2.TimerTask实现 3.ScheduledExecutorService实现
Android线程池管理的代码例子。用于演示普通线程池ThreadPoolExecutor、定时器线程池ScheduledExecutorService等功能。
线程池执行者 ThreadPoolExecutor,定时执行者服务 ScheduledExecutorService, 使用 ForkJoinPool 进行分叉和合并,锁 Lock,读写锁 ReadWriteLock 原子性长整型 AtomicLong,原子性引用型 AtomicReference 修改数据:...
Java多线程入阶干货分享 1.使用线程的经验:设置名称、响应中断、使用ThreadLocal 2.Executor:ExecutorService和...8.定时器:ScheduledExecutorService、大规模定时器TimerWheel 9.并发三大定律 10.图书、相关网络资源
高并发编程第三阶段11讲 AtomicXXXFieldUpdater源码分析及使用场景分析.mp4 高并发编程第三阶段12讲 sun.misc.Unsafe介绍以及几种Counter方案性能对比.mp4 高并发编程第三阶段13讲 一个JNI程序的编写,通过...
高并发编程第三阶段11讲 AtomicXXXFieldUpdater源码分析及使用场景分析.mp4 高并发编程第三阶段12讲 sun.misc.Unsafe介绍以及几种Counter方案性能对比.mp4 高并发编程第三阶段13讲 一个JNI程序的编写,通过...
1、使用线程的经验:设置...8、定时器: ScheduledExecutorService、大规模定时器TimerWheel 9、并发三大定律:Amdahl、Gustafson、Sun-Ni 10、神人和图书、相关网络资源 11、业界发展情况: GPGPU、OpenCL 12、复习题
内容列表 1、使用线程的经验:设置名称、...8、定时器: ScheduledExecutorService、大规模定时器TimerWheel 9、并发三大定律:Amdahl、Gustafson、Sun-Ni 10、神人和图书 11、业界发展情况: GPGPU、OpenCL 12、复习题