2

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?

4

3 回答 3

3
于 2012-06-12T12:24:31.883 回答
1

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

于 2012-06-12T11:09:27.583 回答
0

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

于 2012-06-12T11:07:41.733 回答