Java 一个优于Timer的定时器——ScheduledThreadPoolExecutor
2013-11-05 09:51
309 查看
TimeUnit 时间单元
换算进制:
2) 以指定格式快速实现线程停滞 1) 可以快速实现时间格式转换
toNanos、toMicros、toMillis、toSeconds、toMinutes、toHours、toDays
sleep
TimeUnit.SECONDS.sleep(1); <=> Thread.sleep(1000) // 线程等待1秒
2. ScheduledThreadPoolExecutor
1) ScheduledThreadPoolExecutor是在JDK1.5以后提供可以实现定时、重复运行任务的功能,类似Timer,优于Timer
2) 构造方法
a) ScheduledThreadPoolExecutor(int corePoolSize)使用给定核心池大小创建一个新 ScheduledThreadPoolExecutor
b)ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactorythreadFactory)使用给定的初始参数创建一个新对象
b可以提供线程创建工厂
a) schedule(Callable callable, long delay, TimeUnit unit); 延迟delay时间后开始执行callable 3) 调度方法
b) scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); 延迟initialDelay时间后开始执行command,并且按照period时间周期性重复调用,如果command运行时间较长、可能会同时执行(周期时间包括command运行时间)
c) scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); 延迟initialDelay时间后开始执行command,并且按照period时间周期性重复调用,并且保证在上次运行完后才会执行下一次(周期时间不包括command运行时间)
4) 全局线程方法参数
a) setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) 如果value为true,则在主线程shutdown()方法后如果还没有执行(未达到delay的时间),则延迟任务仍然有机会执行,反之则不会执行,直接退出。(针对b、c方法)
b) setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) 如果value为true,则在主线程shutdown()方法后如果还没有执行(未达到delay的时间),则延迟任务仍然有机会执行,反之则不会执行,直接退出。(针对a方法)
3. ScheduledFuture
在执行调度之后返回该对象。它可以判断任务是否已经执行、是否已经取消、取消以后的操作
4. 操作实例
换算进制:
static final long C0 = 1L; // 1微毫秒 static final long C1 = C0 * 1000L; // 1微秒=1000微毫秒 static final long C2 = C1 * 1000L; // 1毫秒=1000微秒 static final long C3 = C2 * 1000L; // 1秒=1000毫秒 static final long C4 = C3 * 60L; // 1分=60秒 static final long C5 = C4 * 60L; // 1时=60分 static final long C6 = C5 * 24L; // 1天=24时
2) 以指定格式快速实现线程停滞 1) 可以快速实现时间格式转换
toNanos、toMicros、toMillis、toSeconds、toMinutes、toHours、toDays
sleep
TimeUnit.SECONDS.sleep(1); <=> Thread.sleep(1000) // 线程等待1秒
2. ScheduledThreadPoolExecutor
1) ScheduledThreadPoolExecutor是在JDK1.5以后提供可以实现定时、重复运行任务的功能,类似Timer,优于Timer
2) 构造方法
a) ScheduledThreadPoolExecutor(int corePoolSize)使用给定核心池大小创建一个新 ScheduledThreadPoolExecutor
b)ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactorythreadFactory)使用给定的初始参数创建一个新对象
b可以提供线程创建工厂
private final static ScheduledThreadPoolExecutor schedual = new ScheduledThreadPoolExecutor(1, newThreadFactory() { private AtomicInteger atoInteger = new AtomicInteger(0); public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("xxx-Thread "+ atoInteger.getAndIncrement()); returnt; } });
a) schedule(Callable callable, long delay, TimeUnit unit); 延迟delay时间后开始执行callable 3) 调度方法
b) scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit); 延迟initialDelay时间后开始执行command,并且按照period时间周期性重复调用,如果command运行时间较长、可能会同时执行(周期时间包括command运行时间)
c) scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); 延迟initialDelay时间后开始执行command,并且按照period时间周期性重复调用,并且保证在上次运行完后才会执行下一次(周期时间不包括command运行时间)
4) 全局线程方法参数
a) setContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value) 如果value为true,则在主线程shutdown()方法后如果还没有执行(未达到delay的时间),则延迟任务仍然有机会执行,反之则不会执行,直接退出。(针对b、c方法)
b) setExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value) 如果value为true,则在主线程shutdown()方法后如果还没有执行(未达到delay的时间),则延迟任务仍然有机会执行,反之则不会执行,直接退出。(针对a方法)
3. ScheduledFuture
在执行调度之后返回该对象。它可以判断任务是否已经执行、是否已经取消、取消以后的操作
4. 操作实例
/** * 定时执行任务 * * */ public class TestTimer implements Runnable { public static void main(String[] args) { new TestTimer(); } /** * 时间调度器 */ private final static ScheduledThreadPoolExecutor schedual = new ScheduledThreadPoolExecutor(1, new ThreadFactory() { private AtomicInteger atoInteger = new AtomicInteger(0); public Thread newThread(Runnable r) { Thread t = new Thread(r); t.setName("xxx-Thread "+ atoInteger.getAndIncrement()); return t; } }); /** * 设置调度shutdown后停止执行任务 */ static{ schedual.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); schedual.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); } /** * 执行结果 */ private ScheduledFuture<?> scheduledFuture; static int count; public TestTimer() { // scheduledFuture = schedual.schedule(this, 20, TimeUnit.SECONDS); // 20秒后执行 System.out.println("调度时间:"+ timeFormater.format(newDate())); // schedual.scheduleAtFixedRate(this, 5, 2, TimeUnit.SECONDS); // 5秒后每隔2秒执行一次(不论上次是否执行完毕) scheduledFuture = schedual.scheduleWithFixedDelay(this, 5, 2, TimeUnit.SECONDS); // 5秒后每隔2秒执行一次(在上次执行完毕后开始) } private final static SimpleDateFormat timeFormater = new SimpleDateFormat("HH:mm:ss:SSS"); public void run() { if(count++ > 5) { System.out.println("执行已经满足5次,本次任务取消!"); scheduledFuture.cancel(false); schedual.shutdown(); return; } System.out.println("运行时间:"+ timeFormater.format(new Date())); System.out.println("当前线程名称:"+ Thread.currentThread().getName()); try{ TimeUnit.SECONDS.sleep(4); } catch(InterruptedException e) { e.printStackTrace(); } System.out.println("完成时间:"+ timeFormater.format(new Date())); System.out.println("---------------------------"); } }
相关文章推荐
- Java ScheduledThreadPoolExecutor 一个优于Timer、TimerTask的定时任务
- 有两种流行Spring定时器配置:Java的Timer类和OpenSymphony的Quartz。
- Java Timer 定时器
- java_Timer_schedule jdk自带定时器
- Java 定时器:java.util.Timer 和 java.util.TimerTask
- java定时器Timer,TimerTask每隔一段时间随机生成数字
- java日期操作 及 Timer定时器
- 使用boost的deadline_timer实现一个异步定时器
- java之认识定时器Timer
- JAVA TIMER定时器
- java定时器的使用(Timer)
- 显示第一个view, 延迟三秒再显示第二个view,使用java的定时器timer实现 需要启动两个activity
- java 简单的定时器Timer
- Java Timer定时器执行任务
- Java实现一个简单的定时器代码解析
- Java 定时器(Timer,TimerTask)详解及实例代码
- JAVA定时器实现之一(通过继承TimerTask)
- Java核心类库——Timer(定时器)和TimerTask
- Java Timer 定时器的使用
- java timer 定时器