Java多線程—Executor架構概述,java多線程executor
1. 任務Task相關的介面與類1.1 Runnable
表示一個可被執行的命令,通常用於在不同線程中執行任務。
package java.lang;public interface Runnable { public void run();}
1.2 Callable<V>
表示一個有返回結果的任務
package java.util.concurrent;public interface Callable<V> { V call() throws Exception;}
1.3 Future<V>
表示一個非同步任務的結果。
package java.util.concurrent;public interface Future<V> { /** * 取消任務的執行。 * 以下幾種情況不能成功取消任務: * 1.任務已完成。 2.任務已經被取消。 3.任務因為某些原因不能被取消。 * * 如果成功過,任務將不會執行。 * 如果任務已經啟動,參數mayInterruptIfRunning將決定執行任務線程是否應該被中斷,已達到停止任務的目的。 * * 該方法返回後,isDone()將總是返回true。 * 如果該方法返回true,isCanelled()將返回true。 */ boolean cancel(boolean mayInterruptIfRunning); /** * 返回true表示任務在完成前被取消。 */ boolean isCancelled(); /** * 返回true表示任務完成 * * 任務完成可能原因如下: * 1.正常執行完成 2.異常 3.被取消 */ boolean isDone(); /** * 等待任務的完成,並返回結果。 * * @return 返回任務執行結果 * @throws CancellationException 當任務被取消時 * @throws ExecutionException 當任務執行出現異常時 * @throws InterruptedException 等待過程中線程被中斷時 */ V get() throws InterruptedException, ExecutionException; /** * 在指定時間內等待任務的完成,並返回結果。 * * @param 等待逾時最長時間 * @param 時間單位 * @return 返回任務執行結果 * @throws CancellationException 當任務被取消時 * @throws ExecutionException 當任務執行出現異常時 * @throws InterruptedException 等待過程中線程被中斷時 * @throws TimeoutException 等待逾時 */ V get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;}
1.4 RunnableFuture<V>
該介面繼承於Runnable和Future。
package java.util.concurrent;public interface RunnableFuture<V> extends Runnable, Future<V> { void run();}
1.5 FutureTask<V>
一個可以取消的非同步任務。該類實現了RunnableFuture介面。
2. Executor架構2.1 Executor
Executor負責執行提交的Runnable任務。這個介面提供了一種將任務提交和任務執行解耦的機制。Executor通常用於代替顯示建立線程。
package java.util.concurrent;public interface Executor { void execute(Runnable command);}
2.2 ExecutorService
該介面繼承於Executor介面。提供了管理聲明周期的方法和提交任務的便利方法。
public interface ExecutorService extends Executor { void shutdown(); List<Runnable> shutdownNow(); 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;}
2.3 Executors
Executors為一個靜態工廠,用於生產Executor、ExecutorService、ScheduledExecutorService、ThreadFactory和Callable。
package java.util.concurrent;public class Executors { public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); } public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); } public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>()); } public static ScheduledExecutorService newSingleThreadScheduledExecutor() { return new DelegatedScheduledExecutorService (new ScheduledThreadPoolExecutor(1)); } public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) { return new ScheduledThreadPoolExecutor(corePoolSize); } public static ThreadFactory defaultThreadFactory() { return new DefaultThreadFactory(); } public static ThreadFactory privilegedThreadFactory() { return new PrivilegedThreadFactory(); } public static <T> Callable<T> callable(Runnable task, T result) { if (task == null) throw new NullPointerException(); return new RunnableAdapter<T>(task, result); } public static Callable<Object> callable(Runnable task) { if (task == null) throw new NullPointerException(); return new RunnableAdapter<Object>(task, null); } /** Cannot instantiate. */ private Executors() {}}
3. UML類圖
著作權聲明:本文為博主原創文章, 轉載請標明出處 http://blog.csdn.net/sun927