Http://www.cnblogs.com/MOBIN/p/5436482.html
Absrtact: Eexecutor, as a flexible and powerful asynchronous execution framework that supports a variety of different types of task execution strategies, provides a standard way to decouple the submission process and execution process of a task, based on the producer-consumer model, where the thread submitting the task is equivalent to the producer, The thread that executes the task is the consumer and uses runnable to represent the task, and the implementation of the executor provides support for the lifecycle, as well as mechanisms for statistical information collection, application management mechanisms, and performance monitoring. 1.Exexctor Introduction Executor UML diagram: (several common interfaces and subclasses)
Executor: An interface that defines a method Executor to receive a Runnable object, whose method is signed Executor (Runnable command), Executorservice: is a more extensive subclass interface than executor, which provides a way to lifecycle management, and a way to track the execution status of one or more asynchronous tasks back to future Default implementation of Abstractexecutorservice:executorservice execution method Scheduledexecutorservice: An interface for scheduled tasks Scheduledthreadpoolexecutor:scheduledexecutorservice implementation, a timed scheduling task thread pool threadpoolexecutor: thread pool, You can create a thread pool and return a Executorservice object by calling executors the following static factory method: The parameter description of the 2.ThreadPoolExecutor constructor Threadpoolexecutor method signature:
public threadpoolexecutor (int corepoolsize, int maximumpoolsize, Long KeepAliveTime, timeunit unit, blockingqueue<runnable> WORKQ Ueue, Threadfactory threadfactory, Rejectedexecutionhandler handler)/ /The latter two parameters are optional parameters
Parameter description: corepoolsize: Number of core threads, if the running thread is less than corepoolsize, create a new thread to perform a new task, even if other threads in the thread pool are idle maximumpoolsize: Maximum number of threads, The number of threads that can be allowed to be created, and the bounds of the corepoolsize and Maximumpoolsize settings automatically adjust the pool size: The number of threads running corepoolsize < < maximumpoolsize: Create a new thread only when the queue is full corepoolsize= the number of threads running = maximumpoolsize: Create a fixed size thread pool KeepAliveTime: If there are more threads than Corepoolsize, Then the idle time for these extra threads exceeds the KeepAliveTime unit:keepalivetime time Unit Workqueue: Save the task's blocking queue, which is related to the size of the thread pool: When the number of threads running is less than corepoolsize, new threads are created directly to perform tasks without further queuing when the number of threads running is equal to or more than Corepoolsize, the join queue is selected when new tasks are added, and threads are not created directly When a queue is full, create a new thread threadfactory with a new task: Create a new thread using Threadfactory, and use defaultthreadfactory to create a thread handle by default: Defines a policy that handles rejected tasks by using Threadpoolexecutor.abortpolicy by default and throws Rejectexecutorexception 3 when the task is rejected. Executors: Provides a series of static factory methods for creating various thread pools newfixedthreadpool: Creating a thread pool of reusable and fixed threads, if all threads in the thread pool are active, The resubmit task waits in the queue until there is an available thread, and if a thread in the thread pool ends because of an exception, the thread pool supplements a new thread. Method Signature:
public static Executorservice newfixedthreadpool (int nthreads) {return
new Threadpoolexecutor (Nthreads, Nthreads,
0L, Timeunit.milliseconds,
//using a FIFO-sorted blocking queue, all corepoolsize threads are busy and new tasks will wait in queues
Linkedblockingqueue<runnable> ());
}
Newsinglethreadexecutor: Creates a single-threaded executor, if the thread ends because of an exception, creates a new thread to continue the subsequent task method signature:
public static Executorservice Newsinglethreadexecutor () {return
new Finalizabledelegatedexecutorservice
// Corepoolsize and Maximumpoolsize are equal to the fixed thread pool size of 1
(New Threadpoolexecutor (1, 1,
0L, Timeunit.milliseconds,
New linkedblockingqueue<runnable> ());
}
Newscheduledthreadpool: Create a thread pool method signature that can be deferred or executed periodically: Example 1: (using Newscheduledthreadpool to simulate the heartbeat mechanism)
1 public class HeartBeat {
2 public static void Main (string[] args) {
3 scheduledexecutorservice Executor = Executors.newscheduledthreadpool (5);
4 Runnable task = new Runnable () {
5 public void Run () {
6 System.out.println ("HeartBeat .........................");
7 }
8 };
9 executor.scheduleatfixedrate (task,5,3, timeunit.seconds); First execution after 5 seconds, and
11} every 3 seconds
Output:
HeartBeat ...............///5 seconds after first output
HeartBeat ...///////////////////////3
Newcachedthreadpool: Create a cacheable thread pool that will be removed if the thread in the thread pool is not used in 60 seconds, or create a new thread method signature when performing new tasks, reusing available threads when available threads are previously created in the thread pool:
public static Executorservice Newcachedthreadpool () {return
new Threadpoolexecutor (0, Integer.max_value,
60L , Timeunit.seconds,
//using the synchronization queue, direct the task to the thread
new synchronousqueue<runnable> ());
Example 2: