Java - ScheduledExecutorService 101

Executors.newScheduledThreadPool

實際返回類型:

    /**
     * Creates a thread pool that can schedule commands to run after a
     * given delay, or to execute periodically.
     * @param corePoolSize the number of threads to keep in the pool,
     * even if they are idle
     * @return a newly created scheduled thread pool
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }

    /**
     * Creates a thread pool that can schedule commands to run after a
     * given delay, or to execute periodically.
     * @param corePoolSize the number of threads to keep in the pool,
     * even if they are idle
     * @param threadFactory the factory to use when the executor
     * creates a new thread
     * @return a newly created scheduled thread pool
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     * @throws NullPointerException if threadFactory is null
     */
    public static ScheduledExecutorService newScheduledThreadPool(
            int corePoolSize, ThreadFactory threadFactory) {
        return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
    }

實例

scheduleAtFixedRate方法:

  • initialDelay設(shè)置方法第一次執(zhí)行前的延時時間, 設(shè)置為0即馬上就執(zhí)行.
  • period用于控制后續(xù)的重復(fù)執(zhí)行.
    該方法的計時從前一次執(zhí)行的開始計時. 比如說第一次執(zhí)行是在第1秒, period1秒, 那么第二次執(zhí)行將會在第2秒開始, 第3次執(zhí)行將會在第3秒開始... 如果出現(xiàn)某次任務(wù)的執(zhí)行時長超過period, 那么需要等待這次執(zhí)行完成才會開始下一次.
    即該方法的下一次執(zhí)行時間為max(上一次執(zhí)行需要花的時間, period).
  • 如果某次執(zhí)行過程中拋出了exception, 那么后續(xù)的執(zhí)行將不會再繼續(xù)了.
    /**
     * 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
     * {@code initialDelay} then {@code initialDelay+period}, then
     * {@code initialDelay + 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.
     *
     * @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 {@code get()} 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);

實例:

    public static void testScheduleAtFixedRate(){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("working at: " + getCurrentTime());
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                }
            }
        };
        ScheduledFuture<?> future = scheduledExecutorService.scheduleAtFixedRate(runnable, 0, 1000, TimeUnit.MILLISECONDS);
        Thread main = Thread.currentThread();
        scheduledExecutorService.schedule(()-> main.interrupt(), 5000, TimeUnit.MILLISECONDS);
        try {
            future.get();
        } catch (InterruptedException | ExecutionException e) {
            future.cancel(true);
        }

    }

輸出:

working at: 23:07:17
working at: 23:07:19
working at: 23:07:21

從輸出可以看出, 第一次執(zhí)行開始于23:07:17, 按照period參數(shù)1000, 第二次執(zhí)行應(yīng)該是在23:07:18, 但是實際是在23:07:19, 因為任務(wù)執(zhí)行一次需要2000毫秒, 所以即使period計時滿足了, 仍然要等待前一次執(zhí)行完成, 才會執(zhí)行下一次.

scheduleWithFixedDelay方法

注釋:

    /**
     * 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 {@code get()} 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);

scheduleAtFixedRate不同的是, scheduleWithFixedDelay方法的delay計時開始于上一次執(zhí)行結(jié)束. 即下一次執(zhí)行的時間為上一次執(zhí)行完畢后再過delay時間.

實例:

    public static void testScheduleWithFixedDelay(){
        Runnable runnable = new Runnable() {
            @Override
            public void run() {
                try {
                    System.out.println("working at: " + getCurrentTime());
                    Thread.sleep(2000);

                } catch (InterruptedException e) {
                }
            }
        };

        ScheduledFuture<?> future = scheduledExecutorService.scheduleWithFixedDelay(runnable, 0, 2000, TimeUnit.MILLISECONDS);
        Thread main = Thread.currentThread();
        scheduledExecutorService.schedule(() -> main.interrupt(), 10, TimeUnit.SECONDS);
        try {
            future.get();
        } catch (InterruptedException | ExecutionException e) {
            future.cancel(true);
        }
    }

輸出:

working at: 23:12:07
working at: 23:12:11
working at: 23:12:15

可以看出下一次執(zhí)行都是在上一次執(zhí)行完畢后的2000毫秒后 (注意一次執(zhí)行花費(fèi)2000毫秒).

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 134,991評論 19 139
  • 網(wǎng)絡(luò)上關(guān)于java定時器的文章真的是錯誤百出,給我的學(xué)習(xí)造成了很大的困擾,Timer根本就沒有線程安全問題,Tim...
    江江的大豬閱讀 1,790評論 0 27
  • Timer 定時器相信都不會陌生,之所以拿它來做源碼分析,是發(fā)現(xiàn)整個控制流程可以體現(xiàn)很多有意思的東西。 在業(yè)務(wù)開發(fā)...
    石先閱讀 6,409評論 2 13
  • 2016.08.02到2016.08.06,我給自己放了一個假期。在這段時間里,我選擇離開杭州,收拾以往的心情,從...
    Jerry_h閱讀 672評論 0 0
  • 盤錦市興隆臺區(qū)生態(tài)園小區(qū),位于市區(qū)東南角,向海大道東側(cè)。園內(nèi)道路縱橫,湖水環(huán)繞,綠樹成蔭,花卉成片。五區(qū),...
    五月蒹葭閱讀 178評論 0 0