Вопрос

How does the handler message queue work? I know for a fact that the message queue is tied to the thread it is initialized in. If i have 2 tasks(each download from the web), and I initiate an async task from the handler,one for each, will the 2 tasks be executed simultaneously?

I just need to understand, how the queue works.. could anyone please help! :)

Это было полезно?

Решение

First of all, AsyncTask can be executed only on the UI thread. So, even if you have two separate handlers (one for each AsyncTask) they should be both associated with the UI thread.

Secondary, several AsyncTask instances may run either simultaneously or one by one. It depends on the API version. It is better to read documentation about this:

public final AsyncTask execute (Params... params)

Executes the task with the specified parameters. The task returns itself (this) so that the caller can keep a reference to it.

Note: this function schedules the task on a queue for a single background thread or pool of threads depending on the platform version. When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting HONEYCOMB, tasks are back to being executed on a single thread to avoid common application errors caused by parallel execution. If you truly want parallel execution, you can use the executeOnExecutor(Executor, Params...) version of this method with THREAD_POOL_EXECUTOR; however, see commentary there for warnings on its use.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top