【1.scheduleAtFixedRate方法】
定时调度线程池: ScheduledExecutorService executor= Executors.newScheduledThreadPool(2);//提供2个定时调度的线程
定时调度线程池: ScheduledExecutorService executor= Executors.newScheduledThreadPool(2);//提供2个定时调度的线程
- 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.
如果一个线程运行的时间大于周期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
、微妙等)