AsyncTask的VS主题VS服务VS装载机装载机、主题、AsyncTask、VS

2023-09-05 10:33:39 作者:清高不是傲■

我稍微感到困惑的机器人的AsyncTask,线程,服务,装载机之间的差异。 我知道它是如何工作的。但我还是不明白什么时候应该怎么使用。

I got slightly confused about the differences between Asynctask, Thread, Services, Loader in Android. I know how it works. But i still don't understand what and when should i use.

我与Android合作了3年,一般仍然使用AsyncTask的所有后台任务(有时线程)。但许多国家的人民说:AsyncTask的是过时的,并且不建议使用它们。此外,他们建议使用robospice或乱射。

I work with Android for 3 years, and generally still use Asynctask for all background tasks (and sometimes Thread). But many peoples says that "Asynctask is outdated", and don't recommend to use them. Also they recommend to use robospice or Volley.

所以,是的AsyncTask真的那么糟糕,我应该使用框架的网络任务是什么?而且我应该使用什么样的背景(未联网)的任务?

So, is Asynctask really so bad and i should use framework for networking tasks? And what should i use for background (not networking) task?

推荐答案

AysncTasks没有过时的多,因为他们是不完整。 除此之外异步任务不要打扰,如果他们的父活动正在运行与否。出于同样的原因,你有检查验证的范围内为空或不是。此外,除非你是用你自己的线程池的Executor这些任务的执行顺序。

AysncTasks are not 'outdated' as much as they are incomplete. Among other things async tasks do not bother if their parent activity is currently running or not. For the same reason why you include checks for verify the context to be null or not. Besides, unless you are using your own Thread Pool Executor these tasks execute serially.

排球试图填补这些差距,主要是关于同步与主线程和线程池。它的表现最佳,如果你希望做的东西,需要平均网络的要求;像一些元数据列表和图像(图片YouTube应用程序请求和Facebook应用程序请求的职位)。

Volley tries to fill in those gaps, mainly concerning syncing up with the main thread and thread pooling. It behaves optimal if you wish to do stuff that requires average network requests; like some meta data list and images(picture the youtube app requests and facebook app requests for posts).

通常情况下一些优势如下:

Typically few advantages of Volley are as follows

它使工作线程通报了活动(主线程) 更容易资源优先,你可以到你的下载请求提供优先。 一个典型的场景将涉及您优先安排形式通过形象。 在有效的请求,高速缓存和内存管理。 扩展 它为您提供了一个选项,放弃请求,如果你的活动是关机或重启。 在简单模式进行数据检索,而不是AsyncTasks。 It keeps the worker thread informed about the activity(Main thread) Easier resource prioritization you could provide priority to your download requests. A typical scenario would involve you giving priority to text over image. Effective request cache and memory management. Extensible It provides you an option to discard your request in-case your activity was shutdown or restarted. Simpler patterns for data retrieval as opposed to AsyncTasks.

排球不尽人意,当涉及到数据流的请求/视频中提到的谷歌I / O。

Volley fares badly when it comes to streaming requests/video as mentioned at Google I/O.

我不完全知道robospice的。 PS:如果你有时间在你手上看到 https://www.youtube.com/watch?v = yhv8l9F44qo

I'm not exactly aware of robospice. Ps: If you have time on your hand see https://www.youtube.com/watch?v=yhv8l9F44qo

下面是一个继续读下去,如果你想进入其他图书馆与基准相同。 Comparison Android的网络图书馆:OkHTTP,改造,排球

Here's a further read if you wish to go into other libraries with benchmarks for the same. Comparison of Android Networking Libraries: OkHTTP, Retrofit, Volley