Java線程池(1)——線程池中的幾個重要類

【內容摘要】

在java中,如果需要進行多線程編程,可以採用java自帶的線程池來實現,線程池對於我們新手來說是一個非常好的選擇,因爲我們可以不用關心線程池中線程是如何調度的,避免在多線程編程過程產生死鎖等問題。在瞭解線程池的使用前,本文首先介紹一下java線程池的內部原理。

【正文】

一、Java線程池中的幾個重要類

1.ThreadPoolExecutor類

ThreadPoolExecutor類是java線程池中最核心的一個類,它提供了四個構造函數如下:

public class ThreadPoolExecutor extends AbstractExecutorService {
    .....
    //構造器1
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue);
    //構造器2
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory);
    //構造器3
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
            BlockingQueue<Runnable> workQueue,RejectedExecutionHandler handler);
    //構造器4
    public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,
        BlockingQueue<Runnable> workQueue,ThreadFactory threadFactory,RejectedExecutionHandler handler);
    ...
}

由構造函數可知,ThreadPoolExecutor繼承了AbstractExecutorService類,構造器中各個參數的含義如下:
(1)corePoolSize:核心池的大小,在創建了線程池後,默認情況下,線程池中並沒有任何線程,而是等待有任務到來才創建線程去執行任務,除非調用了prestartAllCoreThreads()或者prestartCoreThread()方法,從這2個方法的名字就可以看出,是預創建線程的意思,即在沒有任務到來之前就創建corePoolSize個線程或者一個線程。默認情況下,在創建了線程池後,線程池中的線程數爲0,當有任務來之後,就會創建一個線程去執行任務,當線程池中的線程數目達到corePoolSize後,就會把到達的任務放到緩存隊列workQueue當中,後面會詳細介紹該隊列;
(2)maximumPoolSize:線程池最大線程數,它表示在線程池中最多能創建多少個線程;
(3)keepAliveTime:表示線程沒有任務執行時最多保持多久時間會終止。默認情況下,只有當線程池中的線程數大於corePoolSize時,keepAliveTime纔會起作用,直到線程池中的線程數大於corePoolSize,即當線程池中的線程數大於corePoolSize時,如果一個線程空閒的時間達到keepAliveTime,則會終止,直到線程池中的線程數不超過corePoolSize。但是如果調用了allowCoreThreadTimeOut(boolean)方法,在線程池中的線程數不大於corePoolSize時,keepAliveTime參數也會起作用,直到線程池中的線程數爲0;
(4)unit:參數keepAliveTime的時間單位,取值如:TimeUnit.DAYS、TimeUnit.HOURS等
(5)workQueue:一個阻塞隊列,用來存儲等待執行的任務,阻塞隊列有以下幾種選擇:ArrayBlockingQueue、LinkedBlockingQueue、SynchronousQueue。線程池的排隊策略與workQueue有關。
(6)threadFactory:線程工廠,主要用來創建線程;
(7)handler:表示當拒絕處理任務時的策略,有以下四種取值:
□ThreadPoolExecutor.AbortPolicy:丟棄任務並拋出RejectedExecutionException異常。
□ThreadPoolExecutor.DiscardPolicy:也是丟棄任務,但是不拋出異常。
□ThreadPoolExecutor.DiscardOldestPolicy:丟棄隊列最前面的任務,然後重新嘗試執行任務(重複此過程)
□ThreadPoolExecutor.CallerRunsPolicy:由調用線程處理該任務

2.AbstractExecutorService抽象類

ThreadPoolExecutor類繼承了抽象類AbstractExecutorService,其實現簡略如下:

public abstract class AbstractExecutorService implements ExecutorService {     
    protected <T> RunnableFuture<T> newTaskFor(Runnable runnable, T value) {};
    protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {};
    public Future<?> submit(Runnable task) {};
    public <T> Future<T> submit(Runnable task, T result) {};
    public <T> Future<T> submit(Callable<T> task) {};
    private <T> T doInvokeAny(Collection<? extends Callable<T>> tasks,boolean timed, long nanos) throws InterruptedException, ExecutionException, TimeoutException {};
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException {};
    public <T> T invokeAny(Collection<? extends Callable<T>> tasks,long timeout, TimeUnit unit) throws InterruptedException,ExecutionException, TimeoutException {};
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException {};
    public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,long timeout, TimeUnit unit) throws InterruptedException {};
}

3.ExecutorService接口

AbstractExecutorService抽象類實現的是ExecutorService接口,在這個接口中,定義瞭如下方法:

public interface ExecutorService extends Executor {

    void shutdown();
    boolean isShutdown();
    boolean isTerminated();
    boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
    <T> Future<T> submit(Callable<T> task);
    <T> Future<T> submit(Runnable task, T result);
    Future<?> submit(Runnable task);
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException;
    <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks,long timeout, TimeUnit unit)throws InterruptedException;
    <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException;
    <T> T invokeAny(Collection<? extends Callable<T>> tasks,long timeout, TimeUnit unit) throws InterruptedException,ExecutionException, TimeoutException;
}

4.Executor接口

Executor是一個頂層接口,在它裏面只聲明瞭一個方法execute(Runnable),如下:

public interface Executor {
    void execute(Runnable command);
}

從字面意思可以理解,就是用來執行傳進去的任務的;

至此,可以看出Executor、ExecutorService、AbstractExecutorService和ThreadPoolExecutor之間的關係:Executor是一個頂層接口,ExecutorService實現了Executor接口;然後,AbstractExecutorService實現了ExecutorService接口,但並未實現具體內容,最後的具體實現由ThreadPoolExecutor來執行。

【參考文獻】

http://www.cnblogs.com/dolphin0520/p/3932921.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章