AsyncTask线程永不消亡 [英] AsyncTask threads never die

查看:61
本文介绍了AsyncTask线程永不消亡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用AsyncTask s来获取数据,以响应用户按下按钮.这样可以很好地工作,并在获取数据时使接口保持响应,但是当我检查Eclipse调试器中发生的情况时,我发现每次创建新的AsyncTask时(这很常见,因为它们只能(仅使用一次),则创建了一个新线程,但从未终止.

I'm using AsyncTasks 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线程永不消亡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆