简体   繁体   English

AsyncTask和Thread之间的真正区别

[英]Real difference between AsyncTask and Thread

I have been reading Android documentation ( AsyncTask , Thread ) and vogella tutorial about this matter, but I have doubts yet. 我一直在阅读关于此事的Android文档( AsyncTaskThread )和vogella教程 ,但我还有疑问。

For example, I want to send a message from an Android app to a server. 例如,我想从Android应用程序向服务器发送消息。 And I would like this process to be responsive. 我希望这个过程能够做出回应。 What should I use? 我该怎么用?

I have seen examples where they create a new Thread for not block UI, but this way we don't have the progress of process, also you have to process the response within the Thread because the run() method doesn't returning anything. 我见过他们创建一个新的Thread for not block UI的例子,但是这样我们没有进程的进度,你也必须在Thread处理响应因为run()方法没有返回任何东西。

AsyncTask seems better option than Thread , but I don't know what are the consequences of using an AsyncTask instead of a Thread . AsyncTask似乎比Thread更好,但我不知道使用AsyncTask而不是Thread的后果是什么。

Please read this blog 请阅读此博客

http://crazyaboutandroid.blogspot.in/2011/12/difference-between-android.html http://crazyaboutandroid.blogspot.in/2011/12/difference-between-android.html

and Details are: 和细节是:

Difference between Android Service,Thread,IntentService and AsyncTask Android Service,Thread,IntentService和AsyncTask之间的区别

When to use ? 什么时候用?

Service 服务

   Task with no UI, but shouldn't be too long. Use threads within service for long tasks.

Thread 线

- Long task in general.

- For tasks in parallel use Multiple threads (traditional mechanisms)

AsyncTask 的AsyncTask

- Small task having to communicate with main thread.

- For tasks in parallel use multiple instances OR Executor 

All other answers here are not complete, there is a big difference between AsyncTask and Thread, ie 这里的所有其他答案都不完整,AsyncTask和Thread之间存在很大差异,即

Thread can be triggered from any thread, main(UI) or background; 线程可以从任何线程,主(UI)或后台触发; but AsyncTask must be triggered from main thread. 但必须从主线程触发AsyncTask。

Also on lower API of Android(not sure, maybe API level < 11), one instance of AsyncTask can be executed only once. 此外,在Android的较低API(不确定,可能API级别<11),AsyncTask的一个实例只能执行一次。

For more information read Difference between Android Service, Thread, IntentService and AsyncTask 有关更多信息,请阅读Android服务,线程,IntentService和AsyncTask之间的区别

In general 一般来说

Thread 线

  • Long task in general. 一般的长期任务。

  • For tasks in parallel use Multiple threads (traditional mechanisms) 对于并行使用的任务多线程(传统机制)

AsyncTask 的AsyncTask

  • Small task having to communicate with main thread. 小任务必须与主线程通信。

  • For tasks in parallel use multiple instances OR Executor 对于并行任务,使用多个实例或Executor

一般使用2这个功能是等价的,但AsyncTask在与GUI集成方面更简单

AsyncTask enables proper and easy use of the UI thread. AsyncTask可以正确,方便地使用UI线程。 This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers. 此类允许执行后台操作并在UI线程上发布结果,而无需操作线程和/或处理程序。

You can control its own functions 您可以控制自己的功能

doInBackground(Params... params), onCancelled(), onPostExecute(Result result), onPreExecute(), nProgressUpdate(Progress... values), publishProgress(Progress... values) doInBackground(Params ... params),onCancelled(),onPostExecute(Result result),onPreExecute(),nProgressUpdate(Progress ... values),publishProgress(Progress ... values)

  • I would prefer to Use Async Task as it will let you know when the background process gets started and over and when can I parse the response. 我更喜欢使用异步任务,因为它会让你知道background process何时开始和结束,以及何时可以parse响应。
  • Async has methods like onPreExecute and onPostExecute which will allow us to do tasks before and after calling the background tasks. AsynconPreExecuteonPostExecute等方法,它们允许我们在调用后台任务之前和之后执行任务。

AsyncTask enables proper and easy use of the UI thread. - from Developer . - 来自开发者

The thing is - AsyncTask is a special kind of Thread - one which is a GUI thread, it works in the background and also let's you do something with the GUI - it is basically "pre-programmed" for you with functions onPreExecute(), do inBackground(), onPostExecute() . 事情是--AsyncTask是一种特殊的线程 - 一个是GUI线程,它在后台运行,也让你用GUI做一些事情 - 它基本上是“预编程”的,功能onPreExecute(), do inBackground(), onPostExecute()

In order to make Thread work that way, you have to write a loooot of code. 为了使Thread这种方式工作,你必须编写一个代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM