understanding parallel Async Task in android

前端 未结 2 817
庸人自扰
庸人自扰 2020-12-20 10:14

In my application I am using Async tasks in many places, recently I am facing problem where doInBackground of the below Async task is not getting called, this is happening w

2条回答
  •  星月不相逢
    2020-12-20 10:32

    Hi thanku Stallion and royB for replay....i found what was causing the problem...

    criteria for multiple AsynC task is as below,

    Before Donut (Till 1.6 Version)

    There was no concept of Multiple AsyncTask.
    

    After Donut and till Honeycomb (1.6 – 3.0)

    Here it uses multiple AsyncTask in parallel by default and you cannot customize it.
    

    After Honeycomb and till Jelly Bean (Version 3.0 – 4.3.1)

    private static final int CORE_POOL_SIZE = 5;
    private static final int MAXIMUM_POOL_SIZE = 128;
    private static final BlockingQueue sPoolWorkQueue = new LinkedBlockingQueue(10);
    

    From KitKat (4.4 Above):

    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 BlockingQueue sPoolWorkQueue = new LinkedBlockingQueue(128);
    

    As i am running on Android version: 4.4.3 ,i used Runtime.getRuntime().availableProcessors(); to print CPU_COUNT in my logs it showed 2 so 2+1=3 is the CORE_POOL_SIZE for my device, as i can see from studio debugger console below,

    simultaneously only 3 threads can run in my device and as those threads were already occupied(running).. as i was using while loop in doInbackground() method, those Async Task was never getting finished so new AsyncTask will go to queue and was not getting the Thread to execute. now i have made sure that Async Task are properly terminated as soon as its work is over. and after my fix u can see below that Async Tasks(Threads) are available(in wait state) to take up the new Task...

提交回复
热议问题