Android Threads and thread pool-----thread pool (ii) "Android Development Art and exploration"

Source: Internet
Author: User

The thread pool in Android

Benefits of the thread pool:

1 reusing threads in a thread pool to avoid the performance overhead of thread creation and destruction
2 can effectively control the maximum number of concurrent, avoid a large number of threads because they like to rob resources and cause blocking
3 ability to easily manage threads, provide timed execution and specify interval time for loop execution, etc.

Android line Cheng from Java executor,executor is an interface, the true implementation is threadpoolexecutor.
Threadpoolexecutor provides parameters for configuring thread pooling.

The following is a common construction method:

 public  threadpoolexecutor (int  Span style= "color: #000000;" > Corepoolsize,  int   Maximumpoolsize,  long   KeepAliveTime, timeunit unit, Blockingqueue  & L T             Runnable> WorkQueue, threadfactory threadfactory) {  this   (Corepoolsize, maximumpoolsize, ke  Epalivetime, Unit, WorkQueue, Threadfactory, DefaultHandler); 

The number of core threads in the corepoolsize thread pool
The maximum number of threads that can be accommodated in the maximumpoolsize thread pool, and subsequent tasks are blocked when the active thread reaches this number
KeepAliveTime when non-core threads are idle, longer than this, non-core threads are recycled
The unit is used to specify the time units for the KeepAliveTime parameter.
WorkQueue the task queue in the thread pool
Threadfactory Thread Factory

Threadpoolexecutor the rules that are generally followed when performing a task:
1 if the number of threads in the thread pool does not reach the number of core threads, a core thread is started directly to perform the task.
2 If the number of threads in the thread pool has reached or exceeds the number of core threads, then the task is inserted into the task queue and queued for execution
3 If a task cannot be inserted into the task queue in 2, it is often because the task queue is full, this time if the number of threads does not reach the maximum specified by the thread pool, a non-core thread is started to perform the task immediately
4 If the number of 3 threads reaches the maximum specified by the thread pool, then rejecting this task Threadpoolexecutor calls Rejectedexecution's rejectedexecution to notify the caller

This is the configuration of the Asynctask thread pool:

     Private Static Final intCpu_count =runtime.getruntime (). Availableprocessors (); Private Static Final intCore_pool_size = Cpu_count + 1;//number of core threads equals CPU cores +1        Private Static Final intMaximum_pool_size = Cpu_count * 2 + 1;//the thread pool's maximum number of threads is twice times the number of CPU cores +1        Private Static Final intkeep_alive = 1;//core thread No timeout mechanism, non-core line blocks until those 1 seconds           Private Static FinalThreadfactory sthreadfactory =Newthreadfactory () {Private FinalAtomicinteger MCount =NewAtomicinteger (1);  PublicThread Newthread (Runnable r) {return NewThread (R, "Asynctask #" +mcount.getandincrement ());      }          }; //Task Task Queue capacity     Private Static FinalBlockingqueue<runnable> Spoolworkqueue =NewLinkedblockingqueue<runnable> (128);  Public Static FinalExecutor Thread_pool_executor=NewThreadpoolexecutor (core_pool_size, Maximum_pool_size, Keep_alive, Timeunit.seconds, SPoolWor  Kqueue, sthreadfactory); 

Here's a look at the thread pool classification
The executors class creates a thread pool:

Fixedthreadpool Executors class Newfixedthreadpool created: Public StaticExecutorservice Newfixedthreadpool (intnthreads) {          return NewThreadpoolexecutor (Nthreads, Nthreads,0L, Timeunit.milliseconds,NewLinkedblockingqueue<runnable>()); }   Public StaticExecutorservice Newfixedthreadpool (intnthreads, Threadfactory threadfactory) {          return NewThreadpoolexecutor (Nthreads, Nthreads,0L, Timeunit.milliseconds,NewLinkedblockingqueue<runnable>(), threadfactory); }  
Cachedthreadpool Public StaticExecutorservice Newcachedthreadpool () {return NewThreadpoolexecutor (0, Integer.max_value,60L, Timeunit.seconds,NewSynchronousqueue<runnable>()); }    Public Staticexecutorservice Newcachedthreadpool (threadfactory threadfactory) {return NewThreadpoolexecutor (0, Integer.max_value,30), Timeunit.seconds,NewSynchronousqueue<runnable>(), threadfactory); }  
Scheduledthreadpool Public StaticScheduledexecutorservice Newsinglethreadscheduledexecutor () {return NewDelegatedscheduledexecutorservice (NewScheduledthreadpoolexecutor (1)); }       Public StaticScheduledexecutorservice Newscheduledthreadpool (intcorepoolsize, Threadfactory threadfactory) {          return NewScheduledthreadpoolexecutor (corepoolsize, threadfactory); }  
Singlethreadexecutor Public StaticExecutorservice Newsinglethreadexecutor () {return NewFinalizabledelegatedexecutorservice (NewThreadpoolexecutor (1, 1,                                      0L, Timeunit.milliseconds,NewLinkedblockingqueue<runnable>())); }       Public Staticscheduledexecutorservice newsinglethreadscheduledexecutor (threadfactory threadfactory) {return NewDelegatedscheduledexecutorservice (NewScheduledthreadpoolexecutor (1, threadfactory)); }  

Android Threads and thread pool-----thread pool (ii) "Android Development Art and exploration"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.