当使用的AsyncTask以及何时使用线程在Android的 [英] When to use AsyncTask and When to use Thread in Android

查看:89
本文介绍了当使用的AsyncTask以及何时使用线程在Android的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用的AsyncTask ,何时使用既做工作背景,两者都可以在操纵控制 UI线程通过某种机制。

When to use AsyncTask and When to use Thread as both do work in background and both can manipulate controls in UI Thread by some mechanism..

推荐答案

愿这可以帮助您:

对于长时间运行的任务,我们使用Java线程,和Android的原生的AsyncTask。

For long-running tasks, we use Java threads, and Android's native AsyncTask.

基本上使用的AsyncTask为:

  • 在简单的网络操作不要求下载大量数据的
  • 磁盘密集型任务,可能超过几毫秒采取更多

和使用Java线程:

    涉及中度到大量数据(无论是上载或下载)
  • 网络操作
  • 这需要在后台运行
  • 高CPU任务
  • 要控制相对于GUI线程的CPU使用情况的任何任务
  • Network operations which involve moderate to large amounts of data (either uploading or downloading)
  • High-CPU tasks which need to be run in the background
  • Any task where you want to control the CPU usage relative to the GUI thread

有关详细信息,请参阅 莫希特的回答点击此处

For more information refer Mohit's answer Click Here

服务就像是一个活动,但没有接口。也许,如果你想获取比如你不会为它创建一个空活动的天气,为此,你将使用一个服务。服务是获得一个Context对象具有独立的生命周期。这允许由许多活动重用共同code和,在公共或公开的服务,在许多应用的情况下....

Service is like an Activity but has no interface. Probably if you want to fetch the weather for example you won't create a blank activity for it, for this you will use a Service. Service is access to a Context object which has an independent life cycle. This allows for reuse of common code by many activities and, in the case of public or exposed services in many applications....

A 主题是一个线程,可能你已经从另一部分知道这一点。你要知道,你不能从一个线程更新UI。您需要使用一个处理程序,这和停止线程的某个时候成为问题也。线程是一个机制,做工作,而阻止其他工作......

A Thread is a Thread, probably you already know it from other part. You need to know that you cannot update UI from a Thread. You need to use a Handler for this and stopping a thread sometime become problematic also. A thread is a mechanism for doing work without blocking other work...

服务并不意味着线程和线程并不意味着服务。两者都是从不同的海誓山盟..

这是的AsyncTask 是建议要使用的智能线程。智能,因为它可以帮助它的方法,并且有两个是UI线程,这是很好的更新UI组件上运行的方法。

An AsyncTask is an intelligent Thread that is advised to be used. Intelligent as it can help with it's methods, and there are two methods that run on UI thread, which is good to update UI components.

这篇关于当使用的AsyncTask以及何时使用线程在Android的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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