android中的异步任务与线程 [英] Asynctask vs Thread in android

查看:39
本文介绍了android中的异步任务与线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 UI 中,为了执行一些后台工作,我使用了一个单独的 Thread.但正如其他人所建议的,我现在正在使用 AsyncTask.

In UI, to perform some background work, I used a separate Thread. But as suggested by others, I am now using AsyncTask.

ThreadAsyncTask 之间的主要区别是什么?

What is the main difference between a Thread and an AsyncTask?

在哪种情况下,我应该使用 Thread 还是 AsyncTask?

In which scenario, should I use a Thread or an AsyncTask?

推荐答案

对于长时间运行或 CPU 密集型的任务,基本上有两种方法可以做到:Java 线程和 Android 的原生 AsyncTask.

For long-running or CPU-intensive tasks, there are basically two ways to do this: Java threads, and Android's native AsyncTask.

没有一个一定比另一个更好,但知道何时使用每个调用对于利用系统性能为您带来好处至关重要.

Neither one is necessarily better than the other, but knowing when to use each call is essential to leveraging the system's performance to your benefit.

将 AsyncTask 用于:

  1. 简单的网络操作,不需要下载大量数据
  2. 可能需要超过几毫秒的磁盘绑定任务

将 Java 线程用于:

  1. 涉及中到大量数据(上传或下载)的网络操作
  2. 需要在后台运行的高 CPU 任务
  3. 任何您想要控制相对于 GUI 线程的 CPU 使用率的任务

互联网上有很多很好的资源可以帮助您:

And there are lot of good resources over internet which may help you:

http://www.vogella.com/articles/AndroidBackgroundProcessing/article.html

这篇关于android中的异步任务与线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆