且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AsyncTask的线程仍然存在执行后,这是否正常?

更新时间:2023-01-23 21:32:18

AsyncTask的使用线程池技术。你开始每个AsyncTask的进入队列;有一些空闲线程中的池(或它们根据需要达到一定的极限创建)等待的任务。从池中的空闲线程需要你的AsyncTask并执行它,然后返回到池中。然后,该过程重复,直到没有更多的任务在队列中。

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. 无开销创建线程每次
  2. 在的情况下的巨大任务数的系统性能下降的正常:大部分的任务将在队列中等待,只有少数人会 可以在一个时间执行;最终他们都将得到执行。 否则,如果一个单独的线程开始为每个任务,所述 系统可能会耗尽内存或线程,或任务将 永远结束。
  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.

你你的AsyncTask结束后,在DDMS看到的线程池中的空闲线程。

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