Executorフレームワークと主要メンバー(一)

6933 ワード

ここでは、Executor:Executorの構造とExecutorフレームワークに含まれるメンバーコンポーネントの2つのセクションに分けて説明します.
1、Executorフレームワークの構造
Executorは主に3大部分からなる.
1.1、任務.タスクを実行するために実装する必要があるインタフェースを含む:RunnableインタフェースまたはCallableインタフェース.
1.2、任務の実行.タスク実行メカニズムのコアインタフェースExecutorと、Executorインタフェースから継承されたExecutorServiceインタフェースが含まれます.Executorには、ExecutorServiceインタフェース(ThreadPoolExecutorとScheduledThreadPoolExecutor)を実現する2つのキークラスがあります.
1.3、非同期計算の結果.インタフェースFutureとFutureインタフェースを実装するFutureTaskクラスが含まれます.
これらのクラスとインタフェースの概要を次に示します.
Executorは、タスクのコミットとタスクの実行を分離するExecutorフレームワークの基礎となるインタフェースです.
ThreadPoolExecutorは、コミットされたタスクを実行するスレッドプールのコア実装クラスです.
ScheduledThreadPoolExecutorは、指定された遅延後にコマンドを実行したり、定期的にコマンドを実行したりする実装クラスです.SchduledThreadPoolExecutorはTimerよりも柔軟で、機能が強い.
FutureインタフェースとFutureインタフェースを実現するFutureTaskクラスは、非同期計算の結果を表す
RunnableインタフェースとCallableインタフェースの実装クラスは、いずれもThreadPoolExecutorまたはSchduledThreadPoolExecutorによって実行できます.
2、Executorフレームワークのメンバー
Executorフレームワークの主なメンバー:ThreadPoolExecutor、S h e d u l edThreadPoolExecutor、Futureインタフェース、Runnableインタフェース、Callableインタフェース、Executorsについて説明します.
2.1、ThreadPoolExecutor
ThreadPoolExecutorは通常、ファクトリクラスExecutorsを使用して作成されます.Executorsは、3種類のThreadPoolExecutor:SingleThreadExecutor、FixThreadPool、CachedThreadPoolを作成できます.
この3種類のThreadPoolExecutorをそれぞれご紹介します.
1)FixedThreadPool.次はExecutorsが提供する、固定スレッド数を使用したFixedThreadPoolのAPIを作成する
public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>(),
                                  threadFactory);
}

FixedThreadPoolは、リソース管理のニーズを満たすために現在のスレッド数を制限する必要があるアプリケーションシーンに適しており、負荷の重いサーバに適しています.
2)SingleThreadExecutor.次はExecutorsが提供する、単一スレッドを使用したSingleThreadExecutorのAPIの作成です.
public static ExecutorService newSingleThreadExecutor() {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
    return new FinalizableDelegatedExecutorService
        (new ThreadPoolExecutor(1, 1,
                                0L, TimeUnit.MILLISECONDS,
                                new LinkedBlockingQueue<Runnable>(),
                                threadFactory));
}

SingleThreadExecutorは、各タスクの順序を保証する必要がある場合に適用されます.また,任意の時点では,複数のスレッドがアクティブなアプリケーションシーンであることはない.
3)CachedThreadPool.必要に応じて新しいスレッドを作成するCachedThreadPoolのAPIを作成するExecutorsが提供しています.
public static ExecutorService newCachedThreadPool() {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
    return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                  60L, TimeUnit.SECONDS,
                                  new SynchronousQueue<Runnable>(),
                                  threadFactory);
}

CachedThreadPoolは、短期間の非同期タスクを多く実行するウィジェットや、負荷の軽いサーバに適したサイズのスレッドプールです.
2.2、SchduledThreadPoolExecutor
SchduledThreadPoolExecutorは通常、ファクトリクラスExecutorsを使用して作成されます.Executorsは、次のように2つのタイプのSchduledThreadPoolExecutorを作成できます.
SchduledThreadPoolExecutor.複数のスレッドを含むSchduledThreadPoolExecutor.
SingleThreadSchduledExecutor.1つのスレッドのみを含むSchduledThreadPoolExecutor.
1)SchduledThreadPoolExecutor.次はExecutorsが提供する、固定個数のスレッドSchduledThreadPoolExecutorを作成するAPIです.
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
    return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(
        int corePoolSize, ThreadFactory threadFactory) {
    return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}

SchduledThreadPoolExecutorは、複数のバックグラウンドスレッドが実行するサイクルタスクを必要とし、リソース管理のニーズを満たすためにバックグラウンドスレッドの数を制限するシーンに適しています.
2)SingleThreadSchduledExecutor.次は、Executorsが提供する、単一スレッドを作成するSingleThreadSchduledExecutorのAPIです.
public static ScheduledExecutorService newSingleThreadScheduledExecutor() {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1));
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
    return new DelegatedScheduledExecutorService
        (new ScheduledThreadPoolExecutor(1, threadFactory));
}

SingleThreadSchduledExecutorは、単一のバックグラウンドスレッドでサイクルタスクを実行する必要があり、各タスクを順次実行する必要があるシーンに適しています.
3、Futureインタフェース
FutureインタフェースとFutureインタフェースを実装するFutureTaskクラスは、非同期計算の結果を表すために使用されます.RunnableインタフェースまたはCallableインタフェースの実装クラスをThreadPoolExecutorまたはSchduledThreadPoolExecutorにコミットすると、ThreadPoolExecutorまたはSchduledThreadPoolExecutorはFutureTaskオブジェクトを返します.以下、対応するAPI
public Future<?> submit(Runnable task) {
    return e.submit(task);
}
public <T> Future<T> submit(Callable<T> task) {
    return e.submit(task);
}
public <T> Future<T> submit(Runnable task, T result) {
    return e.submit(task, result);
}

4、RunnableとCallableインタフェース
RunnableおよびCallableインタフェースの実装クラスは、hreadPoolExecutorまたはSchduledThreadPoolExecutorによって実行することができる.これらの違いは、Runnableは結果を返さず、Callableは結果を返すことができます.
Callableインタフェースを実装するオブジェクトを独自に作成できるほか、ファクトリクラスExecutorsを使用してRunnableをCallableにパッケージすることもできます.
次はExecutorsが提供しているRunnableをCallableのAPIに包装しています
public static Callable<Object> callable(Runnable task) {
    if (task == null)
        throw new NullPointerException();
    return new RunnableAdapter<Object>(task, null);
}

次はExecutorsが提供し、Runnableと返される結果をCallableのAPIにパッケージします.
public static <T> Callable<T> callable(Runnable task, T result) {
    if (task == null)
        throw new NullPointerException();
    return new RunnableAdapter<T>(task, result);
}

CallableオブジェクトをThreadPoolExecutorまたはSchduledThreadPoolExecutorにコミットすると、summit()はFutureTaskオブジェクトを返します.私たちはFutureTaskを実行することができます.get()は、タスクの実行が完了するのを待つ.任務が完了したらget()は、タスクの結果を返します.
明记:良い记忆性は腐った笔头に及ばないで、以上の内容は《Java并行プログラミングの芸术》から来ます