Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

multithreading - 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;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        while (true) {
            if (someCondition) {
                Log.D(TAG,"hi hello")
                break;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                //e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        tTask=null;
    }

    @Override
    protected void onCancelled() {
        super.onCancelled();
        System.out.println("onCancelled " + this.isCancelled());
    }

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

i am executing above task as,

 StatusTask  tTask = new StatusTask(device);   
 tTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When you are running AsyncTask multiple times, they all run in a Queue on the same thread, Thus until 1 task didn't finish, the next will NOT start.

Because you never finish the Task...the next task will not start

Have a look at AsyncTask video


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...