定时调度线程池scheduleAtFixedRate和schedule方法

来源:互联网 发布:企业淘宝账户怎么注册 编辑:程序博客网 时间:2024/05/29 04:07
【1.scheduleAtFixedRate方法
定时调度线程池:
ScheduledExecutorService executor=Executors.newScheduledThreadPool(2);//提供2个定时调度的线程
executor.scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
Parameters:
command the task to execute
initialDelay the time to delay first execution 线程第一次执行的初始时延
period the period between successive executions 两个连续线程的周期
unit the time unit of the initialDelay and period parameters

Note: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 initialDelay then initialDelay+period, theninitialDelay + 2 * period, 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.

创建和启动一个周期的线程,该线程第在初始delay时间后第一次启动,然后周期性的执行(周期:period)。
如果一个线程运行的时间大于周期period,后面的线程可能会晚一点启动,但不会同时执行。

【2.schedule方法
executor.schedule(Runnable command,long delay, TimeUnit unit)
@param command the task to execute 进行调度的线程
@param delay the time from now to delay execution 延迟时间
@param unit the time unit of the delay parameter 时间单位(毫秒MILLISECONDS、微妙等)

0 0
原创粉丝点击