AsyncTask 和 Thread 的真正区别 [英] Real difference between AsyncTask and Thread
问题描述
我一直在阅读 Android 文档(AsyncTask、线程) 和 vogella 教程关于这个问题,但我还有疑问.
I have been reading Android documentation (AsyncTask, Thread) and vogella tutorial about this matter, but I have doubts yet.
例如,我想从 Android 应用程序向服务器发送消息.我希望这个过程能够响应.我应该用什么?
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?
我见过他们为非阻塞 UI 创建一个新的 Thread
的例子,但是这样我们就没有进程的进度,你也必须在 中处理响应Thread
因为 run()
方法不返回任何东西.
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.
AsyncTask
似乎比 Thread
更好,但我不知道使用 AsyncTask
而不是 的后果是什么>线程
.
AsyncTask
seems better option than Thread
, but I don't know what are the consequences of using an AsyncTask
instead of a Thread
.
推荐答案
请阅读此博客
http://crazyaboutandroid.blogspot.in/2011/12/android.html之间的差异
和详细信息是:
Android Service、Thread、IntentService 和 AsyncTask 的区别
何时使用?
服务
Task with no UI, but shouldn't be too long. Use threads within service for long tasks.
主题
- Long task in general.
- For tasks in parallel use Multiple threads (traditional mechanisms)
异步任务
- Small task having to communicate with main thread.
- For tasks in parallel use multiple instances OR Executor
这篇关于AsyncTask 和 Thread 的真正区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!