Java multithreading-Executor framework overview, java multithreading executor
1. Task-related interfaces and Classes1.1 Runnable
Indicates an executable command, which is usually used to execute tasks in different threads.
package java.lang;public interface Runnable { public void run();}
1.2 Callable <V>
Indicates a task with returned results.
package java.util.concurrent;public interface Callable<V> { V call() throws Exception;}
1.3 Future <V>
The result of an asynchronous task.
Package java. util. concurrent; public interface Future <V> {/*** cancel the task execution. * In the following cases, the task cannot be canceled: * 1. The task has been completed. 2. The task has been canceled. 3. The task cannot be canceled for some reason. ** If it succeeds, the task will not be executed. * If the task has been started, the mayInterruptIfRunning parameter determines whether the execution thread should be interrupted, and the task has been stopped. ** After this method is returned, isDone () always returns true. * If this method returns true, isCanelled () returns true. */Boolean cancel (boolean mayInterruptIfRunning);/*** returns true, indicating that the task is canceled before completion. */Boolean isCancelled ();/*** returns true, indicating that the task has been completed **. The possible reasons are as follows: * 1. normal execution completed 2. exception 3. canceled */boolean isDone ();/*** wait until the task is completed and return the result. ** @ Return returns the task execution result * @ throws CancellationException when the task is canceled * @ throws ExecutionException when the task execution is abnormal * @ throws InterruptedException when the thread is interrupted */ V get () throws InterruptedException, ExecutionException;/*** wait for the completion of the task within the specified time and return the result. ** @ Param: maximum wait time-out time * @ param time unit * @ return task execution result * @ throws CancellationException when the task is canceled * @ throws ExecutionException when the task is abnormal * @ throws InterruptedException when the thread is interrupted * @ throws TimeoutException waits for timeout */V get (long timeout, timeUnit) throws InterruptedException, ExecutionException, TimeoutException ;}
1.4 RunnableFuture <V>
This interface inherits from Runnable and Future.
package java.util.concurrent;public interface RunnableFuture<V> extends Runnable, Future<V> { void run();}
1.5 FutureTask <V>
An asynchronous task that can be canceled. This class implements the RunnableFuture interface.
2. Executor framework2.1 Executor
Executor is responsible for executing submitted Runnable tasks. This interface provides a mechanism to decouple task submission and task execution. Executor is usually used instead of displaying the creation thread.
package java.util.concurrent;public interface Executor { void execute(Runnable command);}
2.2 ExecutorService
This interface inherits from the Executor interface. Provides methods for managing the Declaration cycle and convenient methods for submitting tasks.
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 is a static factory used to produce Executor, ExecutorService, ScheduledExecutorService, ThreadFactory, and 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 class diagram
Copyright statement: This article is the original author article, reprint please indicate the source http://blog.csdn.net/sun927