HandlerThread 优于其他类似类的最佳使用 [英] Best use of HandlerThread over other similar classes

查看:28
本文介绍了HandlerThread 优于其他类似类的最佳使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解使用 HandlerThread 的最佳用例.

I am trying to understand the best use case of using HandlerThread.

根据定义:

用于启动具有 looper 的新线程的便捷类.looper 可用于创建处理程序类.请注意,仍然必须调用 start()."

"Handy class for starting a new thread that has a looper. The looper can then be used to create handler classes. Note that start() must still be called."

我可能错了,但我可以通过使用 ThreadLooperHandler 实现类似的功能.那么什么时候应该使用HandlerThread?举个例子会很有帮助.

I may be wrong but similar functionality I can achieve by using a Thread, Looper and Handler. So when should I use HandlerThread? An example would be really helpful.

推荐答案

这是一个真实的例子,其中 HandlerThread 变得很方便.当您注册相机预览帧时,您会在 onPreviewFrame() 回调中收到它们.documentation 解释说 此回调在事件线程 open(int) 是从调用的.

Here is a real life example where HandlerThread becomes handy. When you register for Camera preview frames, you receive them in onPreviewFrame() callback. The documentation explains that This callback is invoked on the event thread open(int) was called from.

通常,这意味着回调将在主 (UI) 线程上调用.因此,处理庞大的像素数组的任务可能会在打开菜单、动画动画甚至在屏幕上打印统计数据时卡住.

Usually, this means that the callback will be invoked on the main (UI) thread. Thus, the task of dealing with the huge pixel arrays may get stuck when menus are opened, animations are animated, or even if statistics in printed on the screen.

简单的解决方案是创建一个 new HandlerThread() 并将 Camera.open() 委托给这个线程(我是通过 post(Runnable) 完成的,你不需要实现Handler.Callback).

The easy solution is to create a new HandlerThread() and delegate Camera.open() to this thread (I did it through post(Runnable), you don't need to implement Handler.Callback).

请注意,相机的所有其他工作都可以照常完成,您不必将 Camera.startPreview()Camera.setPreviewCallback() 委托给处理线程.为了安全起见,我等待等待实际的 Camera.open(int) 完成,然后再继续主线程(或用于调用的任何线程 Camera.open() 更改前).

Note that all other work with the Camera can be done as usual, you don't have to delegate Camera.startPreview() or Camera.setPreviewCallback() to the HandlerThread. To be on the safe side, I wait for the actual Camera.open(int) to complete before I continue on the main thread (or whatever thread was used to call Camera.open() before the change).

所以,如果你从代码开始

So, if you start with code

try {
    mCamera = Camera.open(1);
}
catch (RuntimeException e) {
    Log.e(LOG_TAG, "failed to open front camera");
}
// some code that uses mCamera immediately

首先将其按原样提取到私有方法中:

first extract it as is into a private method:

private void oldOpenCamera() {
    try {
        mCamera = Camera.open(1);
    }
    catch (RuntimeException e) {
        Log.e(LOG_TAG, "failed to open front camera");
    }
}

而不是调用 oldOpenCamera() 只需使用 newOpencamera():

and instead of calling oldOpenCamera() simply use newOpencamera():

private void newOpenCamera() {
    if (mThread == null) {
        mThread = new CameraHandlerThread();
    }

    synchronized (mThread) {
        mThread.openCamera();
    }
}
private CameraHandlerThread mThread = null;
private static class CameraHandlerThread extends HandlerThread {
    Handler mHandler = null;

    CameraHandlerThread() {
        super("CameraHandlerThread");
        start();
        mHandler = new Handler(getLooper());
    }

    synchronized void notifyCameraOpened() {
        notify();
    }

    void openCamera() {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                oldOpenCamera();
                notifyCameraOpened();
            }
        });
        try {
            wait();
        }
        catch (InterruptedException e) {
            Log.w(LOG_TAG, "wait was interrupted");
        }
    }
}

请注意,如果您不访问 mCamera,则整个 notify() -- wait() 线程间通信是不必要的打开后立即在原始代码中.

Note that the whole notify() -- wait() inter-thread communication is not necessary if you don't access mCamera in the original code immediately after opening it.

更新:此处将相同的方法应用于加速度计:单独线程中的加速度计传感器

Update: Here the same approach is applied to accelerometer: Acclerometer Sensor in Separate Thread

这篇关于HandlerThread 优于其他类似类的最佳使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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