AsyncTask thread still there after execute, is that normal?

前端 未结 3 534
无人共我
无人共我 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.

    0 讨论(0)
  • 2021-01-12 20:51

    Both the answers provided here are correct. In addition to that, the status of such threads in the pool will be "wait". This phenomenon could also be observed when using libraries like Okhttp which use connection pool for network operations.

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

    yep this avoids the overhead of killing and restarting the thread when you submit the next AsyncTask

    if you submit another AsyncTask after the first is completed the same thread will be reused for it

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