Limitations of Android Asynctask and source code analysis

Source: Internet
Author: User

Reference: http://blog.csdn.net/hitlion2008/article/details/7983449

2.3 Before the asynctask has a global thread pool the maximum value is 5 more than after waiting to be executed

3.0 added the Excuteonexcutor method to set the custom thread pool. However, the default thread pool is a sequential thread pool that can only perform one task at a time and in order.

Asynctask part of the source code.
Public abstract class Asynctask<params, Progress, result> {private static final String Log_tag = "Asynctask";    private static final int cpu_count = Runtime.getruntime (). Availableprocessors ();    private static final int core_pool_size = Cpu_count + 1;    private static final int maximum_pool_size = Cpu_count * 2 + 1;    private static final int keep_alive = 1; private static final Threadfactory sthreadfactory = new Threadfactory () {private final Atomicinteger MCount = new        Atomicinteger (1);        Public Thread Newthread (Runnable R) {return new Thread (R, "Asynctask #" + mcount.getandincrement ());    }    }; private static final blockingqueue<runnable> Spoolworkqueue = new Linkedblockingqueue<runnable> (128    );     /** * A {@link Executor} that can is used to the execute tasks in parallel. */public static final Executor thread_pool_executor = new Threadpoolexecutor (core_pool_size, maximum_pool_s IZE, Keep_aliVE, Timeunit.seconds, Spoolworkqueue, sthreadfactory);  /** * An {@link Executor} This executes tasks one at a time in serial * order.     This serialization are global to a particular process.    */public static final Executor serial_executor = new Serialexecutor ();    private static final int message_post_result = 0x1;    private static final int message_post_progress = 0x2;    private static final Internalhandler Shandler = new Internalhandler ();    private static volatile Executor sdefaultexecutor = Serial_executor;    Private final Workerrunnable<params, result> Mworker;    Private final futuretask<result> mfuture;        Private volatile Status mstatus = status.pending;    Private final Atomicboolean mcancelled = new Atomicboolean ();    Private final Atomicboolean mtaskinvoked = new Atomicboolean (); private static class Serialexecutor implements Executor {final arraydeque<runnable> mtasks = new arraydeque& Lt Runnable> ();        Runnable mactive; Public synchronized void Execute (final Runnable R) {Mtasks.offer (new Runnable () {public void R                    Un () {try {r.run ();                    } finally {Schedulenext ();            }                }            });            if (mactive = = null) {Schedulenext ();                }} protected synchronized void Schedulenext () {if ((mactive = Mtasks.poll ()) = null) {            Thread_pool_executor.execute (mactive); }}}/** * Indicates the current status of the task.     Each status would be set to only once * during the lifetime of a task.         */public enum Status {/** * indicates the task have not been executed yet.         */PENDING,/** * Indicates the task is running. */RUNNING,/** * indicates that {@link ASynctask#onpostexecute} has finished. */Finished,}/** @hide used to force static handler to be created.    */public static void Init () {shandler.getlooper ();    }/** @hide */public static void Setdefaultexecutor (Executor exec) {sdefaultexecutor = exec; }/** * Creates a new asynchronous task.     This constructor must is invoked on the UI thread. */Public Asynctask () {mworker = new workerrunnable<params, result> () {public Result call () t                Hrows Exception {Mtaskinvoked.set (true);                Process.setthreadpriority (Process.thread_priority_background);            Noinspection unchecked return Postresult (Doinbackground (mparams));        }        }; Mfuture = new Futuretask<result> (mworker) {@Override protected void done () {T                ry {postresultifnotinvoked (get ()); } catch (InterRuptedexception e) {ANDROID.UTIL.LOG.W (Log_tag, E); } catch (Executionexception e) {throw new RuntimeException ("An error occured while executing doinbackg                Round () ", E.getcause ());                } catch (Cancellationexception e) {postresultifnotinvoked (null);    }            }        }; }

View Thread_pool_executor default settings for several parameters

    private static final int cpu_count = Runtime.getruntime (). Availableprocessors ();    private static final int core_pool_size = Cpu_count + 1;    private static final int maximum_pool_size = Cpu_count * 2 + 1;    private static final int keep_alive = 1;

Core Size:cpucount+1

Max Size:cupcount*2 +1

Alivetime:1 sec

Limitations of Android Asynctask and source code analysis

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.