AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes. It also has inconsistent behavior on different versions of the platform, swallows exceptions from doInBackground, and does not provide much utility over using Executors directly.
AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.
AsyncTask 旨在简单和正确的使用UI线程,然而,最常用的用法是集成到UI中。这会导致Context泄漏,丢失回调,或者配置更改时崩溃。它在不同的平台版本上也会有不一致的行为,在doInBackground 中吞掉异常,并且跟直接使用Executors相比,没有太多的实用性。
AsyncTask被设计成一个围绕Thread和Handler的助手类,并不算是1个通用的线程框架。AsyncTasks在理想情况下应该被用来短时操作(最多几秒钟)。如果你需要线程长时间运行,强烈建议你使用 java.util.concurrent 包提供的各种API,例如:Executor, ThreadPoolExecutor和 FutureTask.
AsyncTask是一种Android中的辅助类,用于简化UI线程和后台线程的交互,但其存在Context泄漏、回调丢失、配置变化时的问题,以及平台版本间行为不一致。推荐在长时间运行任务时使用java.util.concurrent包的Executor、ThreadPoolExecutor和FutureTask等API来替代AsyncTask。

1429

被折叠的 条评论
为什么被折叠?



