【初識】-JUC·Executor框架

前言

多線程和併發這兩個東西真的是嚮往已久,老是有一種神祕的感受,想去探索一波,又擔憂水平不夠沒法駕馭。想以讀書筆記的方式來寫,可是又以爲缺乏本身的一些思考;可是在沒有足夠併發編程經驗的狀況下又無法去寫出很深入的東西,畢竟沒有踩過坑。因此在閱讀spring源碼的同時,也想抽點時間來看一看JUC的東西,關於這塊只能說是記錄本身學習JUC的一個過程,嘗試用一些具體的代碼demo來加深理解。因此就把本系列寫成《【 初識】-JUC·XXXX》,用來讓本身打開併發編程的大門。java

JUC

JUC即java.util.concurrent;也就是java提供的併發包。JUC中從包結構上來看主要是:spring

  • java.util.concurrent編程

    在這個包下面主要是線程池、併發集合以及一些併發工具類。線程池相關是圍繞Excetor框架來構建;這也是本文下面部分的重點。多線程

  • java.util.concurrent.atomic併發

    這個包下面是一些原子操做類,算是併發輔助工具類,基本實現依賴於CAS;框架

  • java.util.concurrent.locks異步

    這個從名字就能夠知道它的做用,就是提供鎖。工具

JUC各個模塊的類

  • 總體框架

  • atomic

  • locks

  • 併發集合

  • 併發工具

  • forkJoin學習

    fork-join在JUC中有下面三個類:this

    public class ForkJoinPool extends AbstractExecutorService 複製代碼
    public abstract class ForkJoinTask<V> implements Future<V>, Serializable 複製代碼
    public class ForkJoinWorkerThread extends Thread 複製代碼

Future

Future提供了能夠獲取異步執行結果的方法,區別於Runnable的run方法,run是不提供返回結果的。

public interface Future<V> {
    //取消
    boolean cancel(boolean mayInterruptIfRunning);
    //若是任務完成前被取消,則返回true。
    boolean isCancelled();
    //若是任務執行結束,不管是正常結束或是中途取消仍是發生異常,都返回true。
    boolean isDone();
    //獲取異步執行的結果,若是沒有結果可用,此方法會阻塞直到異步計算完成。
    V get() throws InterruptedException, ExecutionException;
    //獲取異步執行結果,若是沒有結果可用,此方法會阻塞,可是會有時間限制,
    //若是阻塞時間超過設定的timeout時間,該方法將拋出異常。
    V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}
複製代碼

Callable

聲明瞭一個名稱爲call()的方法,同時這個方法能夠有返回值V,也能夠拋出異常

public interface Callable<V> { 
  V call() throws Exception; 
} 
複製代碼

關於Callable和Future的使用通常狀況下都是結合咱們的線程池來使用的。

Executor

Executor接口是線程池實現的頂級接口,其和spring中的BeanFactory所承擔的角色差很少,就是提供頂級的功能約束,具體實現交於不一樣子類來完成。

public interface Executor {
    /** * Executes the given command at some time in the future. The command * may execute in a new thread, in a pooled thread, or in the calling * thread, at the discretion of the <tt>Executor</tt> implementation. * * @param command the runnable task * @throws RejectedExecutionException if this task cannot be * accepted for execution. * @throws NullPointerException if command is null */
    void execute(Runnable command);
}
複製代碼

下面是JUC中Executor框架的總體結構:

ExecutorService

public interface ExecutorService extends Executor {
    //關閉線程池
    void shutdown();
    
    List<Runnable> shutdownNow();
    //是否爲Shutdown狀態
    boolean isShutdown();
    //是否爲Terminated狀態
    boolean isTerminated();
    
    //超過超時時間時,會監測ExecutorService是否已經關閉
    //若關閉則返回true,不然返回false。
    //通常狀況下會和shutdown方法組合使用。
    boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException;
    
    //返回一個Future對象,參數接收的是一個Callable的實現
    //Callable接口中的call()方法有一個返回值,能夠返回任務的執行結果
    //區別於Runnable接口中的run()方法(void修飾,沒有返回值)。
    <T> Future<T> submit(Callable<T> task);
    
    <T> Future<T> submit(Runnable task, T result);
    //返回一個Future對象,經過返回的Future對象,咱們能夠檢查提交的任務是否執行完成了。 
    Future<?> submit(Runnable task);
    
    //返回一個Future的List,其中對應着每一個Callable任務執行後的Future對象。
    <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;
        
        
    //接收參數是一個Callable的集合,
    //返回的是全部Callable集合任務中某一個任務的執行結果
    <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;
}
複製代碼

ExecutorService 再Executor接口的基礎上擴展了對線程池狀態的控制以及提交任務執行的超時控制。線程池的基本功能還不夠完善,不能真正的具有處理具體業務的能力(畢竟是個接口,O(∩_∩)O哈哈~)。

開個篇,慢慢學~

相關文章
相關標籤/搜索