java.util.concurrent翻译----Executor框架--接口ScheduledExecutorService
2016-11-03 22:50
471 查看
基于jdk1.7,如果是1.6,则没有ForkJoinPool
package java.util.concurrent; import java.util.concurrent.atomic.*; import java.util.*; /** * An {@link ExecutorService} that can schedule commands(命令) to run after a given * delay, or to execute periodically(周期性). * 一个 ExecutorService,可安排在给定的延迟后运行或定期执行的命令。 * * <p> The <tt>schedule</tt> methods create tasks with various delays * and return a task object that can be used to cancel or check * execution. The <tt>scheduleAtFixedRate</tt> and * <tt>scheduleWithFixedDelay</tt> methods create and execute tasks * that run periodically until cancelled. * * schedule 方法创建各种延迟任务,并返回一个可用于取消或检查执行的任务对象 * scheduleAtFixedRate和 scheduleWithFixedDelay方法创建并执行某些在取消前一直定期运行的任务 * * <p> Commands submitted using the {@link Executor#execute} and * {@link ExecutorService} <tt>submit</tt> methods are scheduled with * a requested delay of zero. Zero and negative delays (but not * periods) are also allowed in <tt>schedule</tt> methods, and are * treated as requests for immediate execution. * * 用 Executor.execute(java.lang.Runnable) 和 ExecutorService 的 submit 方法所提交的命令, * 通过所请求的 0 延迟进行安排。schedule方法中允许出现0和负数延迟(但不是周期的), * 并将这些视为一种立即执行的请求。 * * <p>All <tt>schedule</tt> methods accept <em>relative(相对)</em> delays and * periods as arguments, not absolute times or dates. It is a simple * matter to transform an absolute time represented as a {@link * java.util.Date} to the required form. For example, to schedule at * a certain future <tt>date</tt>, you can use: <tt>schedule(task, * date.getTime() - System.currentTimeMillis(), * TimeUnit.MILLISECONDS)</tt>. Beware(注意) however that expiration(期满) of a * relative delay need not coincide(相符) with the current <tt>Date</tt> at * which the task is enabled due to network time synchronization * protocols, clock drift, or other factors. * * 所有的schedule方法都接受相对延迟和周期作为参数,而不是绝对的时间或日期。 * 要以Date所表示的的绝对时间转换成要求的形式是很简单的。 * 例如,要调度在未来的Date运行,可以使用 * schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。 * 但是要注意,由于网络时间同步协议、时钟漂移,或者其他因素的存在, * 相对延迟的期满日期不必与启用任务的当前 Date相符 * * The {@link Executors} class provides convenient factory methods for * the ScheduledExecutorService implementations provided in this package. * Executors 类为此包中所提供的 ScheduledExecutorService 实现提供了便捷的工厂方法。 * * <h3>Usage Example</h3> * * Here is a class with a method that sets up a ScheduledExecutorService * to beep every ten seconds for an hour: * 以下是一个带方法的类,它设置了 ScheduledExecutorService ,在 1 小时内每 10 秒钟蜂鸣一次: * * <pre> {@code * import static java.util.concurrent.TimeUnit.*; * class BeeperControl { * private final ScheduledExecutorService scheduler = * Executors.newScheduledThreadPool(1); * * public void beepForAnHour() { * final Runnable beeper = new Runnable() { * public void run() { System.out.println("beep"); } * }; * final ScheduledFuture<?> beeperHandle = * scheduler.scheduleAtFixedRate(beeper, 10, 10, SECONDS); * scheduler.schedule(new Runnable() { * public void run() { beeperHandle.cancel(true); } * }, 60 * 60, SECONDS); * } * }}</pre> * * @since 1.5 * @author Doug Lea */ public interface ScheduledExecutorService extends ExecutorService { /** * Creates and executes a one-shot action that becomes enabled * after the given delay. * 创建并执行在给定延迟后启用的一次性操作 * * @param command the task to execute - 要执行的任务 * @param delay the time from now to delay execution * - 从现在开始延迟执行的时间 * @param unit the time unit of the delay parameter * - 延迟参数的时间单位 * * @return a ScheduledFuture representing pending(待定) completion of * the task and whose <tt>get()</tt> method will return * <tt>null</tt> upon completion * 表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在完成后将返回 null * @throws RejectedExecutionException if the task cannot be * scheduled for execution - 如果无法安排执行该任务 * @throws NullPointerException if command is null */ public ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit); /** * Creates and executes a ScheduledFuture that becomes enabled after the * given delay. * 创建并执行在给定延迟后启用的 ScheduledFuture。 * * @param callable the function to execute - 要执行的功能 * @param delay the time from now to delay execution * - 从现在开始延迟执行的时间 * @param unit the time unit of the delay parameter * - 延迟参数的时间单位 * * @return a ScheduledFuture that can be used to extract(提取) result or cancel * 可用于提取结果或取消的 ScheduledFuture * @throws RejectedExecutionException if the task cannot be * scheduled for execution - 如果无法安排执行该任务 * @throws NullPointerException if callable is null */ public <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, 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 * <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. * * 创建并执行一个在给定初始延迟后首次启用的定期操作,后续操作具有给定的周期; * 也就是将在 initialDelay 后开始执行,然后在 initialDelay+period 后执行, * 接着在 initialDelay + 2 * period 后执行,依此类推。 * 如果任务的任何一个执行遇到异常,则后续执行都会被取消. * 否则,只能通过执行程序的取消或终止方法来终止该任务。 * 如果此任务的任何一个执行要花费比其周期更长的时间,则将推迟后续执行,但不会同时执行。 * * @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 * - initialDelay 和 period 参数的时间单位 * * @return a ScheduledFuture representing pending(挂起,待定) completion of * the task, and whose <tt>get()</tt> method will throw an * exception upon cancellation * 表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在取消后将抛出异常 * @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 * - 如果 period 小于等于 0 */ public 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 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 * - initialDelay 和 delay 参数的时间单位 * * @return a ScheduledFuture representing pending completion of * the task, and whose <tt>get()</tt> method will throw an * exception upon cancellation * 表示挂起任务完成的 ScheduledFuture,并且其 get() 方法在取消后将抛出异常 * @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 * - 如果 delay 小于等于 0 */ public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit); }
相关文章推荐
- java.util.concurrent翻译----Executor框架--接口ExecutorService
- java.util.concurrent翻译----Executor框架--抽象类AbstractExecutorService
- java.util.concurrent翻译----Executor框架--接口Executor
- 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计
- java.util.concurrent翻译----Executor框架--类ThreadPoolExecutor
- 聊聊高并发(三十九)解析java.util.concurrent各个组件(十五) 理解ExecutorService接口的设计
- java.util.concurrent翻译----Executor框架--类ScheduledThreadPoolExecutor
- java定时任务接口ScheduledExecutorService
- 【转】java定时任务接口ScheduledExecutorService
- java.util.concurrent.ExecutorService
- 《java.util.concurrent 包源码阅读》10 线程池系列之AbstractExecutorService
- java定时任务接口ScheduledExecutorService
- 使用java.util.concurrent.ExecutorService实现Java线程异步执行
- java.util.concurrent.Callable, Runnable, Future,ExecutorService介绍
- 聊聊高并发(三十八)解析java.util.concurrent各个组件(十四) 理解Executor接口的设计
- java.util.concurrent.AbstractExecutorService.java
- Java并发编程核心方法与框架-ScheduledExecutorService的使用
- java.util.concurrent学习—Executor框架
- java定时任务接口ScheduledExecutorService
- java.util.concurrent 并发框架,异步执行器 Executor