HandlerThread 与 Executor - 什么时候哪个更合适? [英] HandlerThread vs Executor - When is one more appropriate over the other?

查看:45
本文介绍了HandlerThread 与 Executor - 什么时候哪个更合适?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇是否有时我应该选择 Executor 而不是 HandlerThread.有时一个比另一个更好,还是我真的应该坚持使用 HandlerThread?就我而言,我目前正在侦听 ServerSocket 的连接,并在由 Executor 创建的单独线程上处理每个请求.尽管我举了一个具体的例子,但我实际上只是在寻找一种比另一种更合适的情况.不过,我欢迎对我的设计提出意见.

I'm just curious about whether there are times in which I should choose an Executor over a HandlerThread. Are there times that one is superior over the other, or should I really just stick with the HandlerThread? In my case, I'm currently listening to a ServerSocket for connections, and handling each request on a separate thread created by an Executor. Even though I gave a specific example, I'm really just looking for cases in which one is more appropriate than the other. However, I welcome comments about my design.

推荐答案

Executor 类更强大,可以使用线程池,而每个 Handler 引用一个线程.Executor 允许您获取所有计划任务并根据需要取消它们.另一方面,处理程序不会回答简单的问题,例如有多少任务在等待或给我所有等待任务的参考.我认为 Handler 更受限制的一个原因是,Android 允许您访问它用于 UI 的主处理程序,如果您开始取消操作系统任务,您可能真的会搞砸操作系统.

The Executor class is more powerful and can use a pool of threads, whereas each Handler references a single thread. The Executor allows you to get all the scheduled tasks and cancel them if you'd like. The Handler on the other hand will not answer simple questions such as, how many tasks are waiting or give me a reference to all waiting tasks. I believe one reason that the Handler is more limited is because Android gives you access to the main Handler it uses for the UI and you could really screw up the OS if you started canceling OS tasks.

通常,如果您需要一个线程池或大量电源,请使用 Executor.如果您只需要一个不错的后台线程来一次运行一个任务,请使用 Handler.举个例子,当我想查询我的数据库时,我真的只想一次发生一个查询,我不想生成 ANR,所以我使用在后台线程上运行的处理程序来运行我的查询.

In general if you need a pool of threads or lots of power use the Executor. If you just need a nice background thread to run one task at a time use a Handler. As an example when I want to query my database I only really want one query to occur at a time and I don't want to generate an ANR so I use a Handler running on a background thread to run my queries.

我相信您选择的 executor 听起来很合适,因为您想同时处理多个传入的请求,而一个 Handler 一次只能处理一个.

I believe your choice of executor sounds appropriate since you want to handle multiple incoming requests simultaneously and a Handler can only do one at a time.

更新:如何创建在后台线程上运行的处理程序:

在您的构造函数或 onCreate 中写入以下内容,显然您可以将优先级设置为您喜欢的任何内容:

In your constructor or onCreate write the following, obviously you can set the priority to whatever you like:

public class MyClass {

    private Handler mBgHandler;

    public MyClass() {
        HandlerThread bgThread = new HandlerThread("My-Background-Handler");
        bgThread.start();
        mBgHandler = new Handler(bgThread.getLooper());
    }
}

更新:不要忘记在使用完后退出()或quitSafely()您的HandlerThread否则它将永远等待

这篇关于HandlerThread 与 Executor - 什么时候哪个更合适?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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