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

前端 未结 3 1631
有刺的猬
有刺的猬 2021-01-22 12:05

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.



        
相关标签:
3条回答
  • 2021-01-22 12:39

    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

    0 讨论(0)
  • 2021-01-22 12:52

    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

    0 讨论(0)
  • 2021-01-22 12:58

    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.

    0 讨论(0)
提交回复
热议问题