处理器VS AsyncTask的VS主题 [英] Handler vs AsyncTask vs Thread

查看:121
本文介绍了处理器VS AsyncTask的VS主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我稍微感到困惑在Android的处理程序,AsyncTasks和线程之间的差异。我在计算器看了不少博客和问题在这里。

I got slightly confused about the differences between Handlers, AsyncTasks and Threads in Android. I've read quite a few blogs and questions here in stackoverflow.

处理程序是提供您与UI进行沟通后台线程。更新进度的实例应该通过处理程序来完成。使用处理程序有MessagingQueues的优势,所以,如果你想安排的消息或更新多个UI元素或有重复的任务。

Handlers are background threads that provide you to communicate with the UI. Updating a progressbar for instance should be done via Handlers. Using Handlers you have the advantage of MessagingQueues, so if you want to schedule messages or update multiple UI elements or have repeating tasks.

AsyncTasks是相似的,事实上他们利用处理程序,但不会在UI线程中运行,所以其良好的读取数据,例如获取web服务。稍后,您可以与UI交互。

AsyncTasks are similar, infact they make use of Handlers, but doesn't run in the UI thread, so its good for fetching data, for instance fetching webservices. Later you can interact with the UI.

但主题不能与UI交互,提供更多的基本的线程和你错过AsyncTasks所有的抽象。

Threads however can't interact with the UI, provide more "basic" threading and you miss all the abstractions of AsyncTasks.

不过,我想有一个服务运行的套接字连接。如果这是一个处理器或线程,甚至是AsyncTask的运行?用户界面交互是没有必要的。是否有所作为。在性能方面,我用?

However I would like to have a socket connection run in a service. Should this be run in a handler or a thread, or even an asynctask? UI interaction is not necessary at all. Does it make a difference in terms of performance which I use?

推荐答案

如果你看一下的AsyncTask 来源$ C ​​$ c和处理程序,你会看到他们的code纯粹是用Java编写的。 (当然,也有一些例外,但这不是一个重要的问题。)

If you look at the source code of AsyncTask and Handler, you will see their code is written purely in Java. (Of course, there are some exceptions, but that is not an important point.)

因此​​,有什么神奇的AsyncTask 处理程序。他们只是使您的工作更容易,因为开发人员。

So there is no magic in AsyncTask or Handler. They just make your job easier as a developer.

例如:如果程序A调用方法A()方法A()可以在不同的线程中运行使用程序a。您可以很容易地进行验证:

For example: If Program A calls method A(), method A() could run in a different thread with Program A.You can easily verify it using:

Thread t = Thread.currentThread();    
int id = t.getId();

为什么要使用一个新的线程?你可以谷歌它。很多很多原因。

Why you should use a new thread? You can google for it. Many many reasons.

那么,什么是的AsyncTask 处理程序

的AsyncTask 处理程序是用Java编写的(在内部,他们使用主题),所以一切都可以用处理程序做的AsyncTask ,您可以使用实现主题了。

AsyncTask and Handler are written in Java (internally they use a Thread), so everything you can do with Handler or AsyncTask, you can achieve using a Thread too.

什么处理程序的AsyncTask 真正帮助你?

最明显的原因是调用者线程和辅助线程之间的通信。 (来电主题的:它调用一个线程的工作线程的执行某些任务调用程序线程不一定是UI线程)。当然,你可以在其他方面两个线程之间进行通信,但也有许多缺点(和危险)由于线程安全的问题。

The most obvious reason is communication between the caller thread and the worker thread. (Caller Thread: A thread which calls the Worker Thread to perform some task. A Caller Thread does not necessarily have to be the UI thread). Of course, you can communicate between two threads in other ways, but there are many disadvantages (and dangers) due to thread safety issues.

这就是为什么你应该使用处理程序的AsyncTask 。他们这样做最适合你的工作,你只需要知道什么方法来覆盖。

That is why you should use Handler and AsyncTask. They do most of the work for you, you just need to know what methods to override.

处理程序之间的差异的AsyncTask 是:使用的AsyncTask 时的来电线程的是 UI线程的。 这是Android的文件说:

The difference between Handler and AsyncTask is: Use AsyncTask when Caller thread is a UI Thread. This is what android document says:

的AsyncTask能够适当且易于使用的用户界面线程。这个类   允许在UI上进行后台操作并公布结果   螺纹,而不必操纵线程和/或处理程序

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

我想强调两点:

1)易于使用的用户界面线程(因此,使用时调用者线程是UI线程)。

1) Easy use of the UI thread (so, use when caller thread is UI Thread).

2)无需操纵处理程序。 (意思是:你可以用它代替AsyncTask的处理程序,但AsyncTask的是更容易的选择)。

2) No need to manipulate handlers. (means: You can use Handler instead of AsyncTask, but AsyncTask is an easier option).

有在这个岗位,很多事情我都没有说的是,比如:什么是UI线程,或者为什么更容易。你必须知道每一种背后的一些方法和使用它,你就会彻底明白为什么。

There are many things in this post I haven't said yet, for example: what is UI Thread, or why it's easier. You must know some method behind each kind and use it, you will completely understand why..

@:当你读了Android文件,你会看到:

@: when you read the Android document, you will see:

处理程序,您可以发送和处理信息和Runnable对象   与线程的MessageQueue相关

Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue

他们可能看起来有些古怪。只是了解每个线程都有每个消息队列(如待办事项列表),而线程将每封邮件,做它,直到消息队列为空(就像你完成你的工作,去睡觉)。所以,当处理程序进行通信,它只是给出了一个消息调用程序线程,它会等待过程。复杂?只要记住,处理程序可以与主叫方线程在一个安全的方式进行通信。

They may seem strange at first. Just understand that each thread has each message queue (like a to do list), and the thread will take each message and do it until the message queue is empty (just like you finish your work and go to bed). So, when Handler communicates, it just gives a message to caller thread and it will wait to process. Complicated? Just remember that Handler can communicate with the caller thread in a safe way.

这篇关于处理器VS AsyncTask的VS主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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