Public Methodsvoidexecute(Runnable command)
Executes with zero required delay.
booleangetContinueExistingPeriodicTasksAfterShutdownPolicy()Gets the policy on whether to continue executing existing periodic tasks even when this executor has been .
获取有关在此执行程序已shutdown 的情况下、是否继续执行现有定期任务的策略。
booleangetExecuteExistingDelayedTasksAfterShutdownPolicy()Gets the policy on whether to execute existing delayed tasks even when this executor has been .
获取有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
BlockingQueueReturns the task queue used by this executor.
ScheduledFuture>schedule(Runnable command, long delay, TimeUnit unit)Creates and executes a one-shot action that becomes enabled after the given delay.
Creates and executes a ScheduledFuture that becomes enabled after the given delay.
ScheduledFuture>scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)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, then initialDelay + 2 * period, and so on.
ScheduledFuture>scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit 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.
voidsetContinueExistingPeriodicTasksAfterShutdownPolicy(boolean value)Sets the policy on whether to continue executing existing periodic tasks even when this executor has been .
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有定期任务的策略。
voidsetExecuteExistingDelayedTasksAfterShutdownPolicy(boolean value)Sets the policy on whether to execute existing delayed tasks even when this executor has been .
设置有关在此执行程序已 shutdown 的情况下是否继续执行现有延迟任务的策略。
voidshutdown()Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.
ListAttempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a list of the tasks that were awaiting execution.
Submits a Runnable task for execution and returns a Future representing that task.
Submits a value-returning task for execution and returns a Future representing the pending results of the task.
Future>submit(Runnable task)Submits a Runnable task for execution and returns a Future representing that task.
Protected MethodsModifies or replaces the task used to execute a runnable.
重写该函数来,以用个性化的RunnableScheduledFuture来包装runnable任务,task是ScheduledThreadPoolExecutor提供默认的默认的对runnable的RunnableSheduledFuture包装,即ScheduledFutureTask对象。
Modifies or replaces the task used to execute a callable.
重写该函数来,以用个性化的RunnableScheduledFuture来包装callable任务,task是ScheduledThreadPoolExecutor提供默认的默认的对callable的RunnableSheduledFuture包装,即ScheduledFutureTask对象。
更多内容请参考《ThreadPoolExecutor》和《ScheduledExecutorService》Timer与ScheduledThreadPoolExecutor的区别:
Timer对调度的支持是基于绝对时间的,因此任务对系统时间的改变是敏感的;而ScheduledThreadPoolExecutor支持相对时间。
Timer使用单线程方式来执行所有的TimerTask,如果某个TimerTask很耗时则会影响到其他TimerTask的执行;
而ScheduledThreadPoolExecutor则可以构造一个固定大小的线程池来执行任务。
Timer 不会捕获由TimerTask抛出的未检查异常,故当有异常抛出时,Timer会终止,导致未执行完的TimerTask不再执行,新的 TimerTask也不能被调度;
ScheduledThreadPoolExecutor对这个问题进行了妥善的处理,不会影响其他任务的执行。