1. 程式人生 > >java.util.concurrent.ScheduledExecutorService 介面 原始碼

java.util.concurrent.ScheduledExecutorService 介面 原始碼

執行緒池相關

原始碼:

package java.util.concurrent;

public interface ScheduledExecutorService extends ExecutorService {
    //建立並執行在給定延遲後啟用的一次性操作
    public ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit);

    //建立並執行在給定延遲後啟用的 ScheduledFuture
    public <V> ScheduledFuture<V> schedule(Callable<V> callable,long delay, TimeUnit unit);

    //建立並執行一個在給定初始延遲後首次啟用的定期操作,後續操作具有給定的週期
    //在initialDelay後開始執行,然後之後每隔period執行一次
    public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit);

    //建立並執行一個在給定初始延遲後首次啟用的定期操作,隨後在每一次執行終止和下一次執行開始之間都存在給定的延遲
    public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit);
}

 

介面 ScheduledExecutorService

所有超級介面:

    ExecutorExecutorService

所有已知實現類:

    ScheduledThreadPoolExecutor

    schedule 方法使用各種延遲建立任務,並返回一個可用於取消或檢查執行的任務物件。scheduleAtFixedRate 和 scheduleWithFixedDelay 方法建立並執行某些在取消前一直定期執行的任務。

    用 Executor.execute(java.lang.Runnable) 和 ExecutorService 的 submit 方法所提交的命令,通過所請求的 0 延遲進行安排。

    schedule 方法中允許出現 0 和負數延遲(但不是週期),並將這些視為一種立即執行的請求。

    所有的 schedule 方法都接受相對 延遲和週期作為引數,而不是絕對的時間或日期。將以 

Date 所表示的絕對時間轉換成要求的形式很容易。

    例如,要安排在某個以後的 Date 執行,可以使用:schedule(task, date.getTime() - System.currentTimeMillis(), TimeUnit.MILLISECONDS)。

    注意,由於網路時間同步協議、時鐘漂移或其他因素的存在,因此相對延遲的期滿日期不必與啟用任務的當前 Date 相符。 

 Executors 類為此包中所提供的 ScheduledExecutorService 實現提供了便捷的工廠方法。

用法示例

    以下是一個帶方法的類,它設定了 ScheduledExecutorService ,在 1 小時內每 10 秒鐘蜂鳴一次:

package com.thread;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import static java.util.concurrent.TimeUnit.SECONDS;

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);//建立一個ScheduledFuture 物件例項

        scheduler.schedule(new Runnable() {
            public void run() {
                beeperHandle.cancel(true);//執行過程允許中斷
            }
        }, 60 * 60, SECONDS);
    }
}

從介面 java.util.concurrent.ExecutorService 繼承的方法

 awaitTermination, invokeAll, invokeAll, invokeAny, invokeAny, isShutdown, isTerminated, shutdown, shutdownNow, submit, submit, submit

 從介面 java.util.concurrent.Executor 繼承的方法

 execute

 

schedule

ScheduledFuture<?> schedule(Runnable command,long delay, TimeUnit unit)

    建立並執行在給定延遲後啟用的一次性操作。

    引數:

    command - 要執行的任務

    delay - 從現在開始延遲執行的時間

    unit - 延遲引數的時間單位

    返回:

        表示掛起任務完成的 ScheduledFuture,並且其 get() 方法在完成後將返回 null

    丟擲:

    RejectedExecutionException - 如果無法安排執行該任務

    NullPointerException - 如果 command 為 null

 

schedule

<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit)

    建立並執行在給定延遲後啟用的 ScheduledFuture。

    引數:

    callable - 要執行的功能

    delay - 從現在開始延遲執行的時間

    unit - 延遲引數的時間單位

    返回:

        可用於提取結果或取消的 ScheduledFuture

    丟擲:

    RejectedExecutionException - 如果無法安排執行該任務

    NullPointerException - 如果 callable 為 null

 

scheduleAtFixedRate

ScheduledFuture<?> scheduleAtFixedRate(Runnable command,long initialDelay,long period,TimeUnit unit)

    建立並執行一個在給定初始延遲後首次啟用的定期操作,後續操作具有給定的週期;也就是將在 initialDelay 後開始執行,然後在 initialDelay+period 後執行,接著在 initialDelay + 2 * period 後執行,依此類推。如果任務的任何一個執行遇到異常,則後續執行都會被取消。否則,只能通過執行程式的取消或終止方法來終止該任務。如果此任務的任何一個執行要花費比其週期更長的時間,則將推遲後續執行,但不會同時執行。

    引數:

    command - 要執行的任務

    initialDelay - 首次執行的延遲時間

    period - 連續執行之間的週期

    unit - initialDelay 和 period 引數的時間單位

    返回:

        表示掛起任務完成的 ScheduledFuture,並且其 get() 方法在取消後將丟擲異常

    丟擲:

    RejectedExecutionException - 如果無法安排執行該任務

    NullPointerException - 如果 command 為 null

    IllegalArgumentException - 如果 period 小於等於 0

 

scheduleWithFixedDelay

ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,long initialDelay,long delay,TimeUnit unit)

    建立並執行一個在給定初始延遲後首次啟用的定期操作,隨後,在每一次執行終止和下一次執行開始之間都存在給定的延遲。如果任務的任一執行遇到異常,就會取消後續執行。否則,只能通過執行程式的取消或終止方法來終止該任務。

    引數:

    command - 要執行的任務

    initialDelay - 首次執行的延遲時間

    delay - 一次執行終止和下一次執行開始之間的延遲

    unit - initialDelay 和 delay 引數的時間單位

    返回:

        表示掛起任務完成的 ScheduledFuture,並且其 get() 方法在取消後將丟擲異常

    丟擲:

    RejectedExecutionException - 如果無法安排執行該任務

    NullPointerException - 如果 command 為 null。

    IllegalArgumentException - 如果 delay 小於等於 0