kumar kumar - 1 year ago 77
Android Question

understanding parallel Async Task in android

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 when I execute the below Async task for the 3rd time (other Async tasks are also running).

protected class StatusTask extends AsyncTask<Void, Void, Void> {
private final BluetoothDevice device;

public StatusTask(BluetoothDevice device) {
this.device = device;

protected Void doInBackground(Void... voids) {
while (true) {
if (someCondition) {
Log.D(TAG,"hi hello")
try {
} catch (InterruptedException e) {
return null;

protected void onPostExecute(Void result) {

protected void onCancelled() {
System.out.println("onCancelled " + this.isCancelled());

protected void onCancelled(Void aVoid) {
System.out.println("onCancelled result " + this.isCancelled());

i am executing above task as,

StatusTask tTask = new StatusTask(device);

I am running on Android version 4.4.3.

  1. How do I find out how many Sync tasks are running currently in my app? Bcz I guess CORE_POOL_SIZE count might be causing the problem.

  2. Or if deadlock or any other situation is happened and blocking new Async task to get executed, is there a way in android studio to identity and kill the other Async tasks which may not be useful?

  3. If I use CustomThreadPoolExecutor as disconnectTask.executeOnExecutor(mCustomThreadPoolExecutor); with more CorePoolSize its working fine but doInBackground() of next Async task which is using AsyncTask.THREAD_POOL_EXECUTOR is not getting called.

Answer Source

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<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(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<Runnable> sPoolWorkQueue = new LinkedBlockingQueue<Runnable>(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,

enter image description here

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...

enter image description here

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download