一个线程和处理器之间的区别是什么 [英] What is the difference between a Thread and a Handler

查看:109
本文介绍了一个线程和处理器之间的区别是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一个线程和处理器之间的差异。是否创建一个新的处理程序创建一个新线程?当一个新的处理程序使用后(),它是创建一个新的线程中运行?请解释

I'm trying to find out the difference between a thread and a handler. Does creating a new handler create a new thread?. When a new handler is run using post(), is it creating a new thread? Please explain

推荐答案

主题是通用的处理任务,可以做最多的事,但有一点他们不能做的是更新的用户界面。

Threads are generic processing tasks that can do most things, but one thing they cannot do is update the UI.

在另一方面处理程序的后台线程,让你的UI线程(更新UI)。

Handlers on the other hand are background threads that allow you to communicate with the UI thread (update the UI).

因此​​,例如,显示敬酒或更新通过消息的进度条(Runnable接口)发送到一个处理程序,但你不能,如果你启动这个可运行的线程。

So for example show a toast or a update a progress bar via a message (Runnable) posted to a handler but you can't if you start this runnable as a thread.

通过处理程序,你也可以拥有像MessageQueuing,调度和重复。

With handler you can also have things like MessageQueuing, scheduling and repeating.

我还没有遇到一个情况下,我需要在Android的线程。

I am yet to encounter a situation where I needed a thread in android.

我一直都用AsyncTasks和处理程序的组合。

I mostly use a combination of AsyncTasks and Handlers.

处理程序的上述任务。

AsyncTasks下载/数据的读取和查询等。

AsyncTasks for download/ data fetching and polling etc.

您可以阅读开发商的文章在这里无痛线程 在Android的多线程处理。

You can read the developer article here "Painless Threading" for more threading in android.

更正: 每一个处理程序实例都与一个单独的线程和线程的消息队列相关联。他们不是在代表自己的线程。如这里

Correction: Each Handler instance is associated with a single thread and that thread's message queue. They are not threads in their own behalf. as described here.

这篇关于一个线程和处理器之间的区别是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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