我应该重写 HandlerThread run() 吗?
我正在尝试使用 HandlerThread 类来管理应用程序中的线程。以下代码运行良好:
public class ThreadA extends HandlerThread
{
private void foo()
{
//do something
}
private void bar()
{
//Do something else
}
@Override
public boolean handleMessage(Message msg) {
switch(msg.what)
{
case 1:
{
this.foo();
break;
}
case 2:
{
this.bar();
break;
}
}
return false;
}
@Override
protected void onLooperPrepared()
{
super.onLooperPrepared();
synchronized (this) {
this.AHandler = new Handler(getLooper(),this);
notifyAll();
}
}
}
1-我应该重写 run() 方法吗?在“经典”线程中,大部分代码位于 run 方法中。
2-让我们想象一下我需要我的 foo() 方法是一个无限的过程(例如获取视频流)。 最好的解决方案是什么?
- 用我的 foo() 代码覆盖运行?
只需在 foo() 中添加 sleep(xxx) 即可:
私有无效foo() { //做某事 睡眠(100); foo(); }
使用延迟消息,例如:
private void foo()
{
//do something
handler.sendEmptyMessageDelayed(1,100);
}
PS:Asynctask 无法满足我的需求,所以不必费心告诉我使用它。
谢谢
I'm trying to use the HandlerThread class to manage thread in my application. The following code is working great :
public class ThreadA extends HandlerThread
{
private void foo()
{
//do something
}
private void bar()
{
//Do something else
}
@Override
public boolean handleMessage(Message msg) {
switch(msg.what)
{
case 1:
{
this.foo();
break;
}
case 2:
{
this.bar();
break;
}
}
return false;
}
@Override
protected void onLooperPrepared()
{
super.onLooperPrepared();
synchronized (this) {
this.AHandler = new Handler(getLooper(),this);
notifyAll();
}
}
}
1- Should i override the run() method ? In a "classic" thread most of the code is located in the run method.
2- Lets imagine i need my foo() method to be a infinite process (getting a video streaming for example).
What's the best solution ?
- Overriding run with my foo() code ?
Simply adding a sleep(xxx) in foo() :
private void foo()
{
//do something
sleep(100);
foo();
}
-Using a delayed message like :
private void foo()
{
//do something
handler.sendEmptyMessageDelayed(1,100);
}
PS : Asynctask will not cover my need , so do not bother to tell me to use it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您没有理解 HandlerThread 的概念。
HandlerThread
旨在实现处理消息的线程。这意味着它在其run()
方法中使用了Looper.loop()
(这就是为什么您不应该重写它)。这反过来意味着您不需要在onHandleMessage()
中休眠以防止线程退出,因为Looper.loop()
已经处理了这个问题。总结一下:
如果您想了解/了解有关
HandlerThread
的更多信息,请阅读Looper
和Handler
类。I think you didn't get the idea of
HandlerThread
.HandlerThread
is designed to implement thread that handles messages. What this means is that it usesLooper.loop()
in itsrun()
method (and that's why you shouldn't override it). This in turn means that you don't need to sleep inonHandleMessage()
in order to prevent thread from exiting, asLooper.loop()
already takes care of this.To summarize:
If you want to learn/undestand more about
HandlerThread
, read aboutLooper
andHandler
classes.您不应该重写 HandlerThread 中的 run 方法,因为这是该类的核心功能实际发生的地方。根据您所展示的内容,我也认为没有理由这样做。
如果你的任务本身是无限的,那么没有什么可以阻止你以这种方式执行它。您可能想要使用 handler.sendEmptyMessageDelayed 的唯一原因是,如果您计划在执行 foo() 时在 HandlerThread 上排队运行其他任务。您推荐的另一种方法将阻止 HandlerThread 处理任何其他消息。话虽这么说,我怀疑有更好的方法来使你的任务无限。
最后,您应该记住停止无限任务并调用 HandlerThread.getLooper().quit() 以确保 HandlerThread 正常停止。
You shouldn't override the run method in the HandlerThread since that is where the core functionality of the class actually occurs. Based on what you are showing, I also see no reason to do so.
If your task itself is infinite, there isn't anything preventing you from having it execute that way. The only reason you might want to use handler.sendEmptyMessageDelayed is if you plan to have other tasks that you want run queued on the HandlerThread while foo() is executing. The other approach you recommended will prevent the HandlerThread from handling any other message. That being said, I suspect there is a better way to make your task infinite.
Finally, you should remember to stop your infinite task and call HandlerThread.getLooper().quit() to make sure your HandlerThread stops nicely.