Several common thread pools in Java
Reprint: https://www.cnblogs.com/sachen/p/7401959.html
original April 14, 2016 23:29:01
- Label:
- Java/
- Thread pool/
- Executor
Overview:
The class used to manipulate threads in the Java built-in API is thread. There are generally two ways to create a thread,
Time-consuming tasks in Android typically require a separate thread to execute, often using a pool of threads to manage the number of threads, reuse, control execution, and cancellation.
Java thread pool
Java provides four thread pools
Newcachedthreadpool:
The thread pool can be cached, if the thread pool length exceeds the processing needs, then the empty thread is reclaimed, otherwise a new thread is created, and the size of the threads can be infinitely large.
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
When the first task is completed while performing the second task, the thread that executes the first task is reused instead of each new thread.
Newfixedthreadpool:
The thread pool, which controls the maximum number of concurrent threads, waits in the queue for the exceeded thread.
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);
The size of a fixed-length pool is best set based on system resources. such as Runtime.getruntime (). Availableprocessors ().
Newscheduledthreadpool:
Fixed-line pool, support timing and periodic task execution, similar to timer.
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);
Usage examples:
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5);//表示延迟1秒后每3秒执行一次。scheduledThreadPool.scheduleAtFixedRate(new Runnable() { @Override public void run() { System.out.println("delay 1 seconds, and excute every 3 seconds"); }}, 1, 3, TimeUnit.SECONDS);
Newsinglethreadexecutor:
Single thread pool, support FIFO, LIFO, priority policy.
ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();
By observing the source code, four of these threads are created to create a threadpoolexecutor. Where Threadpoolexecutor is the implementation class for the Executorservice interface.
Java.util.concurrent
This package is Java's concurrent programming package, which defines three executor interfaces
Executor: A simple interface to run a new task.
Executorservice: extended the Executor interface. Added some methods to manage the life cycle of the executor and the life cycle of the task.
Scheduledexecutorservice: Extended the Executorservice. Support Future and perform tasks on a regular basis.
Implementation classes include: Scheduledthreadpoolexecutor, Threadpoolexecutor.
The four thread pools available in Java, in addition to Scheduledthreadpool, use Scheduledthreadpoolexecutor, others are threadpoolexecutor.
Threadpoolexecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {}
Parameter description
Corepoolsize: The number of core threads for the thread pool.
Maximumpoolsize: The maximum number of threads that the thread pool can hold.
KeepAliveTime: The timeout length when non-core threads are idle. Beyond that time, non-core threads are recycled.
The time unit of the unit:keepalivetime.
WorkQueue: The task queue in the thread pool.
Threadfactory: Thread factory, default value defaultthreadfactory.
Handler: Saturation policy, when the number of thread pools is greater than maximumpoolsize, the processing policy for the rejected task, the default value Threadpoolexecutor.abortpolicy ().
Reference:
Java Concurrency Tutorial (Oracle official profile)
Trinea:java (Android) thread pool
Java Multithreading: Threadpoolexecutor detailed
Concurrent Programming Network-ifeve.com
Thread Pool Threadpoolexecutor Introduction
Several common thread pools in Java