Unstable and synchronized in AsyncTask

In the AsyncTask internal implementation (in the Android SDK) here , sDefaultExecutor declared with the volatile keyword, and the execute() method in SerialExecutor declared with the synchonized .

  • Now, since AsyncTask can only be executed from the user interface thread, and also if we are running an AsyncTask instance, we cannot release the same instance again if the previous instance did not complete execution. So, how can there be cases with multiple threads?
  • Why SerialExecutor have an ArrayDeque ? Because at that time we can have only one task. If we create a new instance of AsyncTask , then usually we do not get a new instance of ArrayDeque , which again has one task to solve?
  • The same thing happens with ThreadPoolExecutor . Why are thread pools required when we can only have one task for a specific AsyncTask instance? One stream is enough for this.
+5
source share
1 answer

we cannot release the same instance again if the previous instance did not complete execution. So, how can there be cases with multiple threads?

There is, for example, the main user interface thread and the worker thread.

Why does SerialExecutor have an ArrayDeque? Because at that time we can have only one task. If we create a new AsyncTask instance, then don't we get a new ArrayDeque instance that again has only one task to process?

It is not true that there can be only one task. A sequential worker can only perform one task at a time, but you can queue several tasks in the main thread, and they are executed one after another in the worker thread.

There are other performers besides the sequential performer, as well as the performer of the thread pool, which you mention later.

The same thing happens with ThreadPoolExecutor. Why are thread pools necessary when we can have only one task for a specific AsyncTask instance? One stream is enough for this.

Your premise of only one task at a time is incorrect. A thread pool executor is useful for running multiple async tasks on separate threads at the same time.

How are taks queues and multiple tasks viewed? Suppose I make an instance of AsyncTask and execute it 5 times. Then, if you work, the other 4 will not start. SO How can I get multiple tasks anyway?

You can execute one instance of AsyncTask only once. But you can send several different AsyncTask instances for execution. Note that such a posting operation ( execute() , etc.) is asynchronous and returns before the async task completes, and you can run additional code in the user interface thread, including sending new async tasks for execution.

For parallel execution, just use executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, ...) .

Is it also true that since SerialExecutor is static, so that only one instance of it will be used in all AsyncTask instances and therefore a queue is required?

Yes, your application has only one sequential executor, and it is shared between all asynchronous tasks.

+2
source

All Articles