1, Executor: top-level interface thread pool, there is only one method
2, ExecutorService: real thread pool Interface
1) void execute (Runnable command): mission / command, no return value, typically used to perform Runnable
2) <T> Future <T> submit (Callable <T> task): perform a task, return values, again generally performed Callable
3) void shutdown (): close the thread pool
3, AbstractExecutorService: the basic implements all the methods of ExecutorService
4, ThreadPoolExecutor : default thread pool implementation class
5, ScheduledThreadPoolExecutor: implement periodic task scheduling thread pool
6, Executors: tools, thread pool class factory for creating and returns of different types of thread pool
1) Executors.newCachedThreadPool (): Creates a thread pool that creates new threads as needed
2) Executors.newFixedThreadPool (n); a reusable fixed number of threads of thread pool
3) Executors.newSingleThreadExecutor (): Only one thread to create a thread pool
4) Executors.newScheduledThreadPool (n): Creates a thread pool that can schedule commands to run after a given delay, or to execute periodically.
7, the thread pool parameters ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { } |
- corePoolSize: the size of the core pool
- After the default, it creates a thread pool thread count is 0, when there are tasks after, it will create a thread to perform the task.
- But when the number of threads in the thread pool reaches corePoolSize, the mission will arrive on the queue waiting.
- maximumPoolSize: maximum number of threads.
- The number of threads will be automatically released between corePoolSize and maximumPoolSize, less corePoolSize not released. When this value is greater than the task will be handled by a handling mechanism discarded.
- keepAliveTime: After keeping up to how long the task will not terminate a thread
- The default is limited to the thread between corePoolSize and maximumPoolSize
- TIMEUNIT:
- keepAliveTime time unit
- BlockingQueue:
- Blocking queue stores tasks waiting to be executed, a number of options, can be sequential queue, the queue chain and the like.
- ThreadFactory
- Thread factory default is DefaultThreadFactory, Executors static inner classes
- RejectedExecutionHandler:
- Strategy when processing tasks denied. If the thread pool thread is already saturated, and the task queue is also full of new tasks should take what strategy.
- For example, an exception is thrown directly discarded, discarding the oldest tasks in the queue, the default is a direct throw.
1, CallerRunsPolicy: If you find that the thread pool is still running, directly run this thread
2, DiscardOldestPolicy: waiting in the queue in the thread pool, a disposable head removed, and then the current thread into them.
3, DiscardPolicy: do nothing
4、AbortPolicy:java默认,抛出一个异常: