您的位置:首页 > 其它

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进行同步。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: