Android AsyncTask 和 Thread 的真正区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11758629/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Real difference between AsyncTask and Thread
提问by Fran b
I have been reading Android documentation (AsyncTask, Thread) and vogella tutorialabout this matter, but I have doubts yet.
我一直在阅读有关此事的Android 文档(AsyncTask、Thread)和vogella 教程,但我仍有疑问。
For example, I want to send a message from an Android app to a server. And I would like this process to be responsive. What should I use?
例如,我想从 Android 应用程序向服务器发送消息。我希望这个过程能够响应。我应该使用什么?
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
为非阻塞 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
,但我不知道使用 aAsyncTask
而不是 a的后果是什么Thread
。
回答by Dwivedi Ji
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
异步任务
- Small task having to communicate with main thread.
- For tasks in parallel use multiple instances OR Executor
回答by Shirish Herwade
All other answers here are not complete, there is a big difference between AsyncTask and Thread, i.e.
这里的所有其他答案都不完整,AsyncTask和Thread之间有很大的区别,即
Thread can be triggered from any thread, main(UI) or background; but AsyncTask must be triggered from main thread.
线程可以从任何线程、main(UI) 或后台触发;但 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 Service、Thread、IntentService 和 AsyncTask 之间的区别
In general
一般来说
Thread
线
Long task in general.
For tasks in parallel use Multiple threads (traditional mechanisms)
一般任务时间长。
对于并行任务使用多线程(传统机制)
AsyncTask
异步任务
Small task having to communicate with main thread.
For tasks in parallel use multiple instances OR Executor
必须与主线程通信的小任务。
对于并行任务,使用多个实例 OR Executor
回答by Alex Klimashevsky
in general using of 2 this features are equivalent, but AsyncTask is more simple in terms of integration with GUI
通常使用 2 这个特性是等效的,但 AsyncTask 在与 GUI 的集成方面更简单
回答by Tuna Karakasoglu
AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations and publish results on the UI thread without having to manipulate threads and/or handlers.
AsyncTask 可以正确且轻松地使用 UI 线程。此类允许在 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)
回答by Haresh Chaudhary
- I would prefer to Use Async Taskas it will let you know when the
background process
gets started and overand when can Iparse
the response. - Asynchas methods like
onPreExecute
andonPostExecute
which will allow us to do tasks before and after calling the background tasks.
- 我更愿意用异步任务,因为它会让你知道什么时候
background process
被启动,并且在和我何时能parse
响应。 - Async有像
onPreExecute
和onPostExecute
这样的方法,它允许我们在调用后台任务之前和之后执行任务。
回答by Michal
AsyncTask enables proper and easy use of the UI thread.
- from Developer.
AsyncTask enables proper and easy use of the UI thread.
- 来自开发人员。
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
这种方式工作,您必须编写大量代码。