AsyncTask 线程永不消亡 [英] AsyncTask threads never die
问题描述
我正在使用 AsyncTask
来获取数据以响应用户按下按钮.这很有效并且在获取数据时保持界面响应,但是当我检查 Eclipse 调试器中发生的事情时,我发现每次创建一个新的 AsyncTask
(这很常见),因为它们只能使用一次),因此正在创建一个新线程但从未终止.
I'm using AsyncTask
s to fetch data in response to the user pressing a button. This works well and keeps the interface responsive while fetching the data, but when I checked out what was going on in the Eclipse debugger, I found out that every time a new AsyncTask
was created (which is quite often, because they can only be used once), a new thread was being created but never terminated.
结果是大量 AsyncTask
线程只是坐在那里.我不确定这在实践中是否存在问题,但我真的很想摆脱那些额外的线程.
The result is a large number of AsyncTask
threads just sitting there. I'm not sure if this is a problem in practice or not, but I'd really like to get rid of those extra threads.
我怎样才能杀死这些线程?
How can I kill these threads?
推荐答案
AsyncTask
管理使用 ThreadPoolExecutor
创建的线程池.它将有 5 到 128 个线程.如果线程数超过 5 个,则这些多余的线程最多会停留 10 秒钟,然后再被移除.(注意:这些数字适用于目前可见的开源代码,因 Android 版本而异).
AsyncTask
manages a thread pool, created with ThreadPoolExecutor
. It will have from 5 to 128 threads. If there are more than 5 threads, those extra threads will stick around for at most 10 seconds before being removed. (note: these figures are for the presently-visible open source code and vary by Android release).
请不要管 AsyncTask
线程.
这篇关于AsyncTask 线程永不消亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!