gpt4 book ai didi

android - AsyncTask 和 Thread 之间的真正区别

转载 作者:IT老高 更新时间:2023-10-28 23:26:44 25 4
gpt4 key购买 nike

我一直在阅读 Android 文档(AsyncTaskThread)和 vogella tutorial关于这件事,但我还有疑问。

例如,我想从 Android 应用程序向服务器发送消息。我希望这个过程能够响应。我应该使用什么?

我见过一些例子,他们为不阻塞 UI 创建了一个新的 Thread,但是这样我们就没有进程的进度,你也必须在 中处理响应线程,因为 run() 方法不返回任何内容。

AsyncTask 似乎比 Thread 更好,但我不知道使用 AsyncTask 而不是 的后果是什么>线程.

最佳答案

请阅读此博客

http://crazyaboutandroid.blogspot.in/2011/12/difference-between-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

关于android - AsyncTask 和 Thread 之间的真正区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11758629/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com