ScheduleExecutorService用法
来源:互联网 发布:machinarium for mac 编辑:程序博客网 时间:2024/05/29 19:07
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进行同步。
0 0
- ScheduleExecutorService用法
- ScheduleExecutorService
- ScheduleExecutorService
- ScheduleExecutorService,周期性线程池
- Java 定时器ScheduleExecutorService实战
- 任务调度ScheduleExecutorService学习心得
- java多线程之-ScheduleExecutorService方法
- android ScheduleExecutorService定时周期执行指定任务
- android ScheduleExecutorService定时周期执行指定任务
- ScheduleExecutorService接口和spring控制定时任务的理解
- 用法
- ,, 用法
- 用法
- 用法
- #用法
- ??用法
- !!用法
- CustDialog用法 SimpleAdapter 用法
- 人工智能史
- iOS程序员常逛得网址
- 地图的实际extent
- 自己实现的集合框架
- java 基础 ->多态
- ScheduleExecutorService用法
- 设置VisualSVN在提交修改时必须输入一定数量的备注信息
- 掐指一算,你命里缺钱
- 【1-2】Linux配置JDK环境的方式
- MSSQL查询某个表在哪些存储过程中使用过
- 魅族云同步的实践-协议和架构
- 后缀数组的应用——重复子串
- English Learn
- 在linux系统下运行c语言程序