ScheduleExecutorService用法
2016-01-04 14:41
330 查看
java.util.concurrent包下有个ScheduleExecutorService接口,查看源码这个接口的作用主要是
An {@link ExecutorService} that can schedule commands to run after a given
delay, or to execute periodically.
意思就是这个service可以录制一些命令在一段时间间隔之后执行,或者周期性的执行。
ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。
这两个方法定义如下:
/**
* Creates and executes a periodic action that becomes enabled first
* after the given initial delay, and subsequently with the given
* period; that is executions will commence after
* <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
* <tt>initialDelay + 2 * period</tt>, and so on.
* If any execution of the task
* encounters an exception, subsequent executions are suppressed.
* Otherwise, the task will only terminate via cancellation or
* termination of the executor. If any execution of this task
* takes longer than its period, then subsequent executions
* may start late, but will not concurrently execute.
*
* @param command the task to execute
* @param initialDelay the time to delay first execution
* @param period the period between successive executions
* @param unit the time unit of the initialDelay and period parameters
* @return a ScheduledFuture representing pending completion of
* the task, and whose <tt>get()</tt> method will throw an
* exception upon cancellation
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if command is null
* @throws IllegalArgumentException if period less than or equal to zero
*/
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
参数意义:
command:执行线程
initialDelay:初始化延时
period:两次开始执行最小间隔时间
unit:计时单位
/**
* Creates and executes a periodic action that becomes enabled first
* after the given initial delay, and subsequently with the
* given delay between the termination of one execution and the
* commencement of the next. If any execution of the task
* encounters an exception, subsequent executions are suppressed.
* Otherwise, the task will only terminate via cancellation or
* termination of the executor.
*
* @param command the task to execute
* @param initialDelay the time to delay first execution
* @param delay the delay between the termination of one
* execution and the commencement of the next
* @param unit the time unit of the initialDelay and delay parameters
* @return a ScheduledFuture representing pending completion of
* the task, and whose <tt>get()</tt> method will throw an
* exception upon cancellation
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if command is null
* @throws IllegalArgumentException if delay less than or equal to zero
*/
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
command:执行线程
initialDelay:初始化延时
period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间)
unit:计时单位
public class SystemClock {
private static final SystemClock instance = new SystemClock();
private final long precision;
private final AtomicLong now;
private ScheduledExecutorService scheduler;
public static SystemClock getInstance() {
return instance;
}
public SystemClock() {
this(1L);
}
public SystemClock(long precision) {
this.precision = precision;
now = new AtomicLong(System.currentTimeMillis());
scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, "System_Clock");
thread.setDaemon(true);
return thread;
}
});
scheduler.scheduleAtFixedRate(new Timer(now), precision, precision, TimeUnit.MILLISECONDS);
}
public long now() {
return now.get();
}
public long precision() {
return precision;
}
protected class Timer implements Runnable {
// 注入进来,避免访问SystemClock.now占用很多CPU
private final AtomicLong now;
private Timer(AtomicLong now) {
this.now = now;
}
@Override
public void run() {
now.set(System.currentTimeMillis());
}
}
上面这段程序是一个线程安全的时间辅助工具类,默认1毫秒会将系统时间和程序中的now进行同步。
An {@link ExecutorService} that can schedule commands to run after a given
delay, or to execute periodically.
意思就是这个service可以录制一些命令在一段时间间隔之后执行,或者周期性的执行。
ScheduleExecutorService接口中有四个重要的方法,其中scheduleAtFixedRate和scheduleWithFixedDelay在实现定时程序时比较方便。
这两个方法定义如下:
/**
* Creates and executes a periodic action that becomes enabled first
* after the given initial delay, and subsequently with the given
* period; that is executions will commence after
* <tt>initialDelay</tt> then <tt>initialDelay+period</tt>, then
* <tt>initialDelay + 2 * period</tt>, and so on.
* If any execution of the task
* encounters an exception, subsequent executions are suppressed.
* Otherwise, the task will only terminate via cancellation or
* termination of the executor. If any execution of this task
* takes longer than its period, then subsequent executions
* may start late, but will not concurrently execute.
*
* @param command the task to execute
* @param initialDelay the time to delay first execution
* @param period the period between successive executions
* @param unit the time unit of the initialDelay and period parameters
* @return a ScheduledFuture representing pending completion of
* the task, and whose <tt>get()</tt> method will throw an
* exception upon cancellation
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if command is null
* @throws IllegalArgumentException if period less than or equal to zero
*/
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay,
long period,
TimeUnit unit);
参数意义:
command:执行线程
initialDelay:初始化延时
period:两次开始执行最小间隔时间
unit:计时单位
/**
* Creates and executes a periodic action that becomes enabled first
* after the given initial delay, and subsequently with the
* given delay between the termination of one execution and the
* commencement of the next. If any execution of the task
* encounters an exception, subsequent executions are suppressed.
* Otherwise, the task will only terminate via cancellation or
* termination of the executor.
*
* @param command the task to execute
* @param initialDelay the time to delay first execution
* @param delay the delay between the termination of one
* execution and the commencement of the next
* @param unit the time unit of the initialDelay and delay parameters
* @return a ScheduledFuture representing pending completion of
* the task, and whose <tt>get()</tt> method will throw an
* exception upon cancellation
* @throws RejectedExecutionException if the task cannot be
* scheduled for execution
* @throws NullPointerException if command is null
* @throws IllegalArgumentException if delay less than or equal to zero
*/
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay,
long delay,
TimeUnit unit);
command:执行线程
initialDelay:初始化延时
period:前一次执行结束到下一次执行开始的间隔时间(间隔执行延迟时间)
unit:计时单位
public class SystemClock {
private static final SystemClock instance = new SystemClock();
private final long precision;
private final AtomicLong now;
private ScheduledExecutorService scheduler;
public static SystemClock getInstance() {
return instance;
}
public SystemClock() {
this(1L);
}
public SystemClock(long precision) {
this.precision = precision;
now = new AtomicLong(System.currentTimeMillis());
scheduler = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
@Override
public Thread newThread(Runnable runnable) {
Thread thread = new Thread(runnable, "System_Clock");
thread.setDaemon(true);
return thread;
}
});
scheduler.scheduleAtFixedRate(new Timer(now), precision, precision, TimeUnit.MILLISECONDS);
}
public long now() {
return now.get();
}
public long precision() {
return precision;
}
protected class Timer implements Runnable {
// 注入进来,避免访问SystemClock.now占用很多CPU
private final AtomicLong now;
private Timer(AtomicLong now) {
this.now = now;
}
@Override
public void run() {
now.set(System.currentTimeMillis());
}
}
上面这段程序是一个线程安全的时间辅助工具类,默认1毫秒会将系统时间和程序中的now进行同步。
相关文章推荐
- 乐观锁&悲观锁
- (四)黑马程序员——抽象类&接口
- Python调用web service
- 掐指一算,你命里缺钱
- 116_扩展欧几里得算法
- 【1-2】Linux配置JDK环境的方式
- 魅族云同步的实践-协议和架构
- English Learn
- SpingMVC ModelAndView, Model,Control以及参数传递
- SAP HANA企业级培训系列课程<第一部分>
- 深入浅出MFC学习笔记 消息
- 自己实现的集合框架
- 资源文件加载
- HTTPS网络请求时plist文件的设置
- 多线程
- MFC 三种消息
- Android中把View转换成bitmap,再缓存到sd卡
- java 接口
- 怎么查看库文件是32位的还是64位的
- (一〇二)内联函数