Android Threading (ii) Asynctask source Analysis (ii)

Source: Internet
Author: User

Reprint Please specify the Source:http://blog.csdn.net/itachi85/article/details/45055365  take a look at this article before reading the previous articlehttp://blog.csdn.net/itachi85/article/details/45041923
thread pool in 3.AsyncTaska total of two thread pools are defined in Asynctask one is that we have introduced the thread pool Serialexecutor, which is currently the thread pool that we call the Asynctask.execute () method by default, which has been mentioned in the previous article, The other is the default thread pool Thread_pool_executor before version 3.0. Now let's review the source code of Serialexecutor:
private static class Serialexecutor implements Executor {        final arraydeque<runnable> mtasks = new Arraydeque< ; Runnable> ();        Runnable mactive;        Public synchronized void Execute (final Runnable R) {            Mtasks.offer (new Runnable () {public                void run () {                    try {
   r.run ();                    } finally {                        schedulenext ();}}            );            if (mactive = = null) {                schedulenext ();            }        }

This default thread pool can handle only one task at a time, and a task is completed before the next task is performed, equivalent to Executors.newsinglethreadpool (). The arraydeque above is a queue that loads runnable, and if we start a lot of tasks at once, we call Arraydeque's offer () when we run the Execute () method for the first time. The Runnable method adds the passed-in object to the end of the queue, and then determines that the Mactive object is not equal to NULL, the first run equals null, and then calls the Schedulenext () method. The Schedulenext () method is also called in Finally, which guarantees that the next task executes every time a task is completed. Let's take a look at the source code of the Schedulenext () method:
  protected synchronized void Schedulenext () {            if ((mactive = Mtasks.poll ())! = null) {                Thread_pool_ Executor.execute (mactive);            }        }

The first value is taken from the head of the runnable queue, and if it is not empty, it is assigned to the Mactive object, and then Thread_pool_executor is called to execute the Runnable object being fetched. Thread_pool_executor Source:
  private static final int core_pool_size = 5;    private static final int maximum_pool_size =;    private static final int keep_alive = 1;  .... public static final Executor thread_pool_executor            = new Threadpoolexecutor (core_pool_size, Maximum_pool_size, KEEP _alive,                    timeunit.seconds, Spoolworkqueue, sthreadfactory);

This is the thread pool before version 3.0, the number of threads that can run at the same time is 5, and the total size of Workqueue is 128. When we start 10 tasks, only 5 tasks are prioritized, the rest of the tasks are placed in Workqueue, and when Workqueue is greater than 128, Rejectedexecutionhandler is called to do the reject processing. Of course, before 3.0 there was no serialexecutor of this class. If you do not want to use the default thread pool we can also use this 3.0 version of the thread pool before
Asynctask.executeonexecutor (asynctask.thread_pool_executor, NULL);

at the same time 3.0 version also provides executeonexecutor this method can be passed into the asynctask definition of the thread pool can also be passed into the executor definition of the 4 thread pool, do not know that the four kinds of thread pool can see http://blog.csdn.net /itachi85/article/details/44874511Incoming Cachedthreadpool:
                Likelisttask mlikelisttask=new likelisttask (); Mlikelisttask.executeonexecutor (Executors.newcachedthreadpool (), NULL);
Of course we can also pass in the custom thread pool:
Executor exec =new threadpoolexecutor (0,  integer.max_value,       0L, Timeunit.milliseconds, new Linkedblockingqueue<runnable> ());    New Likelisttask (). Executeonexecutor (exec, NULL);










Android Threading (ii) Asynctask source Analysis (ii)

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.