AsyncTask的比几秒钟长? [英] AsyncTask for longer than a few seconds?

查看:152
本文介绍了AsyncTask的比几秒钟长?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

API参考状态,

AsyncTasks应理想地用于短操作(在几秒钟   在最。)

AsyncTasks should ideally be used for short operations (a few seconds at the most.)

是一个doInBackground的需要,比如30秒,线程池可能会耗尽线程的问题?而如果是这样的原因,将它不再是,如果我确保我的应用程序将永远不会有不止一个这样的长期运行doInBackground并发执行的问题?

Is the problem with a doInBackground that takes, say, 30 seconds that the thread pool might run out of threads? And if that's the reason, would it cease to be a problem if I ensure my app will never have more than one such long running doInBackground executing concurrently?

推荐答案

我相信AyncTasks表现一般仍依赖于这催生了他们的前台活动叠加,使如如果活动衍生出的AsyncTask,在用户离开应用程序,然后操作系统内存不足,它会杀死活动的过程(包括仍在运行的AsyncTask的),只是希望你能恢复状态,如果重新开始用户恢复/返回到您的应用程序。

I believe that AyncTasks are in general still tied to the foreground activity stack that spawned them, so that e.g. if an Activity spawns an AsyncTask, the user leaves the app, and then the OS is short of memory, it will kill the Activity's process (including the still-running AsyncTask), and just expect you to restore the state and start over if the user resumes/returns to your app.

对于长时间运行的任务,特别是那种在这里只会有只有一个或几个,你可能需要一个服务,而不是,因为那些能坚持,即使你的应用程序的用户界面被关闭,以节省内存。

For longer-running tasks, particularly the sort where there will only be only one or a few, you probably want a Service instead, because those can persist even when your app's UI is shut down to save memory.

免责声明:我没有做过Android的编码在一段时间,所以这个答案可能是过时的或基于对事物如何运作一个有缺陷的理解。我会删除此警告,如果有人有更多最近的经验可以发表评论确认;高代表乡亲们,欢迎只需编辑本段走,而是如果他们知道这是正确的。

Disclaimer: I haven't done Android coding in awhile, so this answer may be out of date or based on a flawed understanding of how things work. I will remove this caveat if someone with more recent experience can comment to confirm; high-rep folks are welcome to just edit this paragraph away instead if they know this is correct.

这篇关于AsyncTask的比几秒钟长?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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