正在运行的线程的时候是什么处理器吗? [英] What does Handler do when running a thread?

查看:86
本文介绍了正在运行的线程的时候是什么处理器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真搞不清楚这一点,但是当我从我的SurfaceView trigging一个线程,发送即时消息处理程序,像这样的构造

I'm really confused about this, but when I'm trigging a thread from my SurfaceView, im sending a Handler with the constructor like this

private static Thread thread;

public SurfaceView(Context localContext) {
      //other stuff
      thread = new Thread(mySurfaceHolder, myEngine, this, new Handler());
      //other stuff
}

在我的线程级的我分配一个处理程序,对象,我从我的观点发送的处理程序,如:

and in my thread-class I assign a Handler-object with the handler I sent from my view, like this:

    public Thread (SurfaceHolder lHolder,
    Engine lEngine,
    View lView,
    Handler lHandler){

    surfaceHolder = lHolder;
    engine = lEngine;
    view = lView;
    handler = lHandler;

}

那么,这是什么处理器吗?我从来没有在任何方面我的线程类使用它,为什么仍然显示出我,我应该送处理程序与构造Web上的例子吗?
我看不到连接...

So what does this Handler do? I never use it in my thread-class in any ways, so why are the examples on the web still showing me that I should send a handler with the constructor? I can't see the connection....

在此先感谢!

推荐答案

我假设你的code要扩展主题,因为没有构造采取的处理程序Thread类。处理程序是用来回传达给UI线程,因为Android的线程模型要求在一个特殊的专用线程执行的UI操作。

I assume in your code you are extending Thread, because there is no constructor taking a handler for the Thread class. The handler is used to communicate back to the UI thread, as the Android threading model requires UI operations to be performed on a special dedicated thread.

在创建一个新的线程,code在后台线程中执行,因此UI的操作是不安全的。如果你不需要做任何事情UI相关的,可以删除的处理程序。

When creating a new thread, the code is executed in a background thread, and thus UI-operations are unsafe. If you do not need to do anything UI-related, you can remove the handler.

由于Android API 3,有 AsyncTask的,这使得通信背景和UI之间容易并减轻需要使用的处理程序。

Since Android API 3, there is AsyncTask, which makes communication between background and UI easier and alleviates the need to use a handler.

例如:


  • 对于使用处理程序的一个例子,
    看到这个

  • 请参见无痛
    线程
    为好,这包含链接使用线程来样的项目。

  • For an example of using a Handler, see this.
  • See Painless Threading as well, which contains links to sample projects using threading.

这篇关于正在运行的线程的时候是什么处理器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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