Executor框架結構與主要成員(一)

本文分兩部分來介紹Executor:Executor的結構和Executor框架包含的成員組件java

一、Executor框架的結構編程

    Executor主要由3大部分組成。小程序

    1.一、任務。包含被執行任務須要實現的接口:Runnable接口或Callable接口。
服務器

    1.二、任務的執行。包括任務執行機制的核心接口Executor,以及繼承自Executor接口的ExecutorService接口。Executor有兩個關鍵類實現了ExecutorService接口(ThreadPoolExecutor和ScheduledThreadPoolExecutor)
併發

    1.三、異步計算的結果。包括接口Future和實現Future接口的FutureTask類。框架

    下面是這些類和接口的簡介。
異步

    Executor是一個接口,它是Executor框架的基礎,它將任務的提交和任務的執行分離開來。
spa

    ThreadPoolExecutor是線程池的核心實現類,用來執行被提交的任務。
線程

    ScheduledThreadPoolExecutor是一個實現類,能夠在給定的延遲後運行命令,或者按期執行命令。SchduledThreadPoolExecutor比Timer更靈活,功能更強大。
code

    Future接口和實現Future接口的FutureTask類,表明異步計算的結果

    Runnable接口和Callable接口的實現類,均可以被ThreadPoolExecutor或SchduledThreadPoolExecutor執行


二、Executor框架的成員

介紹Executor框架的主要成員:ThreadPoolExecutor、ScheduledThreadPoolExecutor、Future接口、Runnable接口、Callable接口、Executors。

2.一、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。下面是Executors提供的,建立一個會根據須要而建立新線程的CachedThreadPool的API

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.二、SchduledThreadPoolExecutor

SchduledThreadPoolExecutor一般使用工廠類Executors來建立,Executors能夠建立2中類型的SchduledThreadPoolExecutor,以下:

SchduledThreadPoolExecutor。包含若干個線程的SchduledThreadPoolExecutor。

SingleThreadSchduledExecutor。只包含一個線程的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適用於須要單個後臺線程執行週期任務,同時須要保證順序地執行各個任務的場景。

三、Future接口

Future接口和實現Future接口的FutureTask類用來表示異步計算的結果,當咱們把Runnable接口或者Callable接口的實現類提交(submit)給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);
}

四、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()來等待任務執行完成。當任務完成後FutureTask.get()將會返回任務的結果。

註明:好記性不如爛筆頭,以上內容來自《Java併發編程的藝術》

相關文章
相關標籤/搜索