one Thread pool usage scenarios

First of all, I don’t want to talk about the concept of threads, I believe you are familiar with it, we are used to it when dealing with time-consuming tasks
To create a child threads to deal with, but when the task is more impossible when each task is to create a task, will cause the system frequency of creation and destruction, take up a lot of resources interface card, may even be problems of abnormal memory, so in order to solve this problem we need a unified management tool thread
The thread pool. Use thread pool for unified management of multi-task threads, reasonable allocation of resources, to avoid unreasonable resource occupation, reuse of threads (this is the focus), the so-called reuse is the thread execution will not immediately destroy, will wait for another task, so that there will not be frequent creation and destruction.

two Common ways to pool threads

Let’s start with the basic construction parameters

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue

workQueue) { throw new RuntimeException(“Stub!” ); }

CorePoolSize: number of core threads in the thread pool.

MaximumPoolSize: specifies the maximum number of threads in the thread pool.

KeepAliveTime: Specifies the timeout period during which a non-core thread is idle before it is reclaimed

Unit: The unit of the time attribute above

WorkQueue: the queue of tasks in the thread pool, through the thread pool

Runnable: The object will be stored in this parameter.

ThreadFactory: a threadFactory that can be used to set thread names, etc. This parameter is generally not required.

2. Four types of thread pools in Android

2.1 FixThreadPool

A FixThreadPool has a fixed number of core threads that are not collected, and the threads are all executing while subsequent tasks are waiting

The constructor

public static ExecutorService newFixedThreadPool(int nThreads) {

return new ThreadPoolExecutor(nThreads, nThreads,

0L, TimeUnit.MILLISECONDS,

new LinkedBlockingQueue<Runnable>());

}

Execute the sample

public void excuteFixThreadPool()

{

ExecutorService fixedThreadPool = Executors.newFixedThreadPool(3);

fixedThreadPool.execute(runnable);

}

2.2 SingleThreadPool

SingleThreadPool has only one core thread and all tasks are executed sequentially in the same thread on a first-in, first-out basis

The constructor

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {

return new FinalizableDelegatedExecutorService

(new ThreadPoolExecutor(1, 1,

0L, TimeUnit.MILLISECONDS,

new LinkedBlockingQueue<Runnable>(),

threadFactory));

}

Execute the sample

public void excuteSingleThreadPool()

{

ExecutorService singleThreadExecutor = Executors.newSingleThreadExecutor();

singleThreadExecutor.execute(runnable);

}

2.3 CachedThreadPool

CachedThreadPool does not have a core thread, but only a fee-core thread. New tasks create new threads. Threads that are idle for more than a specified time are recycled.

The constructor

public static ExecutorService newCachedThreadPool() {

return new ThreadPoolExecutor(0, Integer.MAX_VALUE,

60L, TimeUnit.SECONDS,

new SynchronousQueue<Runnable>());

}

Execute the sample

public void excuteCachedThreadPool()

{

ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

cachedThreadPool.execute(runnable);

}

2.4 ScheduledThreadPool

The number of core threads is fixed. There is no limit to the number of non-core threads (idle threads are immediately recycled).

The constructor

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {

return new ScheduledThreadPoolExecutor(corePoolSize);

}

Execute the sample

public void excuteScheduledThreadPool () { ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(5); scheduledThreadPool.schedule(runnable, 1, TimeUnit.SECONDS); // The task will be executed after 1s

3. Requirements in individual projects and processes implemented using thread pools

I personally have a demand in the project is in enter the main interface for the first time need on the server and copy of the data stored in the local, and all the data more time-consuming tasks, if one needs to wait for a long time, the user experience is really poor, so the thread pool is adopted to improve the management, to perform multiple tasks at the same time, run by a thread pool, In this way, the task processing is parallel, the waiting time is shorter, and the user experience is improved.