问题描述
我正在使用AsyncTask
s来获取数据,以响应用户按下按钮.这样可以很好地工作,并在获取数据时使接口保持响应,但是当我检查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.
如何杀死这些线程?
推荐答案
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线程永不消亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!