问题描述
我是新来的Android编程和我使用的AsyncTask
s到获取数据,以响应用户pressing一个按钮。这种运作良好,并保持在获取数据的界面的响应,但是当我检查出什么在Eclipse调试器是怎么回事,我发现,每次有新的AsyncTask
是创建的(这是经常的,因为它们只能用一次),正在创建新线程,但从来没有终止。其结果是大量的的AsyncTask
线程只是坐在那里。我不知道这是在实践中还是不成问题的,但我真的很想摆脱这些额外的线程。我怎样才能杀死这些线程?
I'm new to Android programming and 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. 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秒被删除之前。 (注意:这些数字为presently可见开源$ C $ C,具体费用由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的主题永远不死(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!