Android Thread Pools

  1. Benefits thread pool
  2. Detailed parameters thread pool
  3. Thread Pool introduced species
  4. Works + strategy threads
  5. Api details thread pool
Why use a thread pool?
  1. Each thread is created, there is a demise of overhead, it can be reused in the thread pool thread to avoid these costs
  2. You can control the maximum number of concurrent threads, improving system resource utilization
  3. It provides a powerful API

Detailed thread pool parameters

corePoolSize: the number of kernel threads. When submitting a task to the thread pool, thread pool will create a core thread to perform the task, even if the other core is idle threads to execute new tasks will be to create a new thread can be understood as the core when the core is equal to the number of threads in the thread pool allows the maximum number of kernel threads of time, if there is a new task, it will not create a new core thread.

maximumPoolSize: thread pool allows you to create the maximum number of threads, if the queue is full, and the number of threads that have been created less than the maximum number of threads, the thread pool will re-create a new thread to perform tasks

keepAliveTime: thread activity holding time, that is, when the thread pool worker thread is idle after time remain viable.

TimeUnit: thread activity hold time the unit: optional units days (DAYS), hours (HOURS), minutes

workQueue: Task Queue: the queue waiting to be executed to save the blocked task
threadFactory: Create a thread factory: you can create for each thread sets out a more meaningful name by thread factory.


Thread Pool Category:

newCachedThreadPool: Only non-core thread, the maximum number of threads is very large, it creates a new thread for a new job when all threads are active, otherwise it will make use of idle threads (60s idle time, after will be recycled, so the thread pool have 0 the thread may) to handle the task. advantages :( any task will be executed immediately, more suitable for the implementation of a large number of less time-consuming task)

newFixedThreadPool: only the core thread, and a fixed number when idle thread pool worker threads are not released, it will take up some system resources. Advantages: faster response to outside request

newScheduledThreadPool: core number of threads fixed, non-core thread (not idle work to do will be recycled immediate) There is no limit .. Advantages: the task and repeating the task execution timing of a fixed period

newSingleThreadExecutor: only one core thread to ensure that all tasks are completed in sequence in the same thread. Pros: do not need to deal with the problem of thread synchronization


Working principle and policy of the thread pool Photo explanation:

Here Insert Picture Description

If more than the maximum number of threads in the pool, four strategies:

1, AbortPolicy: default policy, ran directly abnormalities that prevent normal

2, CallerRunsPolicy: "The caller is running" an adjustment mechanism, the policy will neither abandon the task, it will not throw an exception, but the task back to the initiator, such as main thread (which thread initiated, will return to let which thread to perform tasks)

3, DiscardOldestPolicy: abandon the task longest waiting queue, then the current task to join the queue to try again submit the current job

4, DiscardPolicy: direct discard tasks, without giving any treatment nor ran an exception, if allowed task is lost, this is the best kind of program


Several important thread pool API Profile:

Submit the thread pool tasks:

1, execute the method used to submit the job does not require a return value, it is impossible to determine whether the task is executed successfully thread pool;

2, submit method submits the task requires the return value. Thread pool will return an object of future type, by this future object can determine whether the task is successful,
and can retrieve the value returned by the get method future, the get method blocks the current thread until the task is completed, use get (Long
timeout, TimeUnit unit) method will block the current thread returns immediately after a period of time, this time it is possible to perform the task is not complete.

Close the thread pool
principle: traversing thread pool worker thread, and then one by one thread calls the interrupt method to interrupt the thread, so I can not respond to interrupts may never terminate task.

the shutdown : only the state of the thread pool is set to SHUTWDOWN state, the task being performed will continue execution, the interrupt is not executed.

shutdownNow: state sucked the thread pool is set to STOP, the task being performed were stopped and not be returned to perform tasks

A, isShutdown method returns true as long as the call either of these methods is closed. When all tasks are closed, it means that the thread pool is closed successfully, then call isTerminated method returns true. As for which method should be called to close the thread pool should be determined by the characteristics of the task to submit to the thread pool, usually calling shutdown method to close the thread pool, if you do not have to perform the task finished, you can call shutdownNow method.

Published 51 original articles · won praise 78 · views 30000 +

Guess you like

Origin blog.csdn.net/weixin_39079048/article/details/99701892