What change did really happen in Async Task after Android Gingerbread?

廉价感情. 提交于 2019-12-31 03:32:08

问题


What change did really Android team make in Async task after android 2.3. When I executed the following code I am getting same result in both Android 2.3 and 3.0.

package com.sample.asynctask;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;

public class AsyncTaskTestActivity extends Activity {
    private static final String TAG = "AsyncTaskTestActivity";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        //ExecutorService executorService = Executors.newFixedThreadPool(1);
        for (int i = 1; i <= 20; i++) {
            TestTask testTask = new TestTask(i);
            testTask.execute();
        }
    }

    private static class TestTask extends AsyncTask<Void, Integer, Void> {
        int i;
        public TestTask(int i) {
            Log.i(TAG, "Constructor for " + i);
            this.i = i;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            Log.i(TAG, "onPreExecute for " + i);
        }

        @Override
        protected Void doInBackground(Void... params) {
            Log.i(TAG, i + " Thread goes to sleep");
            try {
                Thread.sleep(20000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            Log.i(TAG, i + " Thread wakes up");
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            Log.i(TAG, "onPostExecute for " + i);
        }
    }
}

My assumption in Gingerbread: 5 Async task executes at one thread pool at a time. My assumption in Honeycomb: 1 Async task executes at one thread pool at a time. Exactly like concurrent execution.

But, both Gingerbread and Honeycomb executes 5 Async tasks at the same time.

And also when the number for Async task is increased to 140.i am not getting java.util.concurrent.RejectedExecutionException .

Whether my assumptions are correct? What is really happening inside there?


回答1:


Whether my assumptions are correct?

Your assumptions is correct, well, sort of.

What is really happening inside there?

The default executor inside android.os.AsyncTask:

... ...

private static volatile Executor sDefaultExecutor = SERIAL_EXECUTOR;

... ...

has been reset in android.app.ActivityThread:

... ...

// If the app is Honeycomb MR1 or earlier, switch its AsyncTask
// implementation to use the pool executor.  Normally, we use the
// serialized executor as the default. This has to happen in the
// main thread so the main looper is set right.
if (data.appInfo.targetSdkVersion <= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) {
    AsyncTask.setDefaultExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}

... ...

What change did really happen in Async Task after Android Gingerbread?

Check out AsyncTask change history, more specifically, this one:

Mar 17, 2011 - AsyncTask now uses the poll executor for apps up through HC MR1 and t…

When the number for Async task is increased to 140, I am not getting java.util.concurrent.RejectedExecutionException.

This is a factor of total number of tasks and execution time per each task, in another world, the total task number is 140 (which is bigger that 128), however, the total number of thread allocated by threadpool at any given time is smaller than 128, in another word, there are always some idle threads (due to last task finished and release resource) available in your case. you can try increase the execution time per each task for example Thread.sleep(10000), this will probably give you RejectedExecutionException.




回答2:


This thread outlines the changes to AsyncTask in ICS. The information is provided by a Google Employee, so it's trustworthy.

https://groups.google.com/forum/?fromgroups#!topic/android-developers/8M0RTFfO7-M




回答3:


The change in the AsyncTask behavior also requires you run on ICS and above hardware.

See: http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html



来源:https://stackoverflow.com/questions/10995281/what-change-did-really-happen-in-async-task-after-android-gingerbread

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!