AsyncTask thread still there after execute, is that normal?

前端 未结 3 562
无人共我
无人共我 2021-01-12 20:21

when I use AsyncTasks checking in the DDMS, the thread persist in memory as waiting thread after the onPostExecute() method, is that normal?. Here is a simplified Activity t

3条回答
  •  耶瑟儿~
    2021-01-12 20:37

    AsyncTask uses "thread pool" technique. Each AsyncTask you start gets into a queue; there are some idle threads in the "pool" (or they are created as needed up to a certain limit) waiting for tasks. An idle thread from the pool takes your AsyncTask and executes it, then returns to the pool. Then the process repeats until there are no more tasks in the queue.

    This approach has 2 important features:

    1. no overhead for creating a thread every time
    2. in case of huge number of tasks system performance degrades gracefully: most of the tasks will wait in the queue and only few of them will be executed at a time; eventually all of them will get executed. Otherwise, if a separate thread was started for each task, the system would likely run out of memory or threads, or tasks will take forever to finish.

    The thread which you see in DDMS after your AsyncTask finished is the idle thread in the pool.

提交回复
热议问题