问题描述
我的应用程序由一个 Activity
组成.在本活动中,我将创建多个 HandlerThread
,它们循环运行以执行套接字阻止操作.
当前,我在 Activity.onDestroy()
期间向所有 HandlerThread
的每个人发布退出消息.
有时候,当我打开我的应用程序,将其关闭并重新启动时,它会崩溃(很多时间是由于将消息发布到未运行的处理程序线程).
我的问题是:关闭我的应用程序时关闭 HandlerThread
的正确方法是什么?(请注意,这些线程可能会在套接字操作上阻塞)./p>
更多信息:我有一个在onCreate中启动的处理程序线程池(第一次启动应用程序时没问题).
每个处理程序可运行循环都用
包装 if(shouldRun){//身体}别的 {关闭();}
声明.
close方法删除所有待处理的消息和可运行对象,并将消息发布到处理程序,这将导致处理程序调用其 looper.quit()
.这样,如果当前的处理程序线程被IO操作阻止,则只有完成它之后,他才会退出().
- 您在那里必须有一些不一致之处,否则您的应用程序将不会崩溃.您确定确实没有运行 HandlerThread 的原因吗?创建活动时不创建HandlerThread对象吗?
- 如果您的HandlerThreads正在等待I/O操作,我将考虑尝试中断它们.只需删除回调和消息并要求Looper退出,甚至将终止消息发送到Handler,也将无济于事.直到Android终止进程(可能会或可能不会发生)之前,HandlerThread对象仍会存在.这意味着您的应用程序将收集可能无法访问的僵尸HandlerThread对象".当然,除非您可以向这些HandlerThreads发送终止消息,该消息到达它们所阻塞的通道.
- 最好重用HandlerThread对象.服务可能是执行此操作的正确模型.
- 此外,如果线程在您的活动中幸存下来,则需要通知他们其通信对等方(您的活动)已消失.否则,他们可能会引用已经消失的东西.
My app is composed of a single Activity
. In this activity, I'm creating multiple HandlerThread
s which run in a loop to do socket blocking operations.
Currently I post a quit message to everyone of these HandlerThread
s during my Activity.onDestroy()
.
Sometimes, when I open my app, close it and relaunch it, it crashes (many time due to posting a message to a handler thread which is not running).
My question is: What is the right way to close HandlerThread
when I close my app? (Note that those threads might be blocking on a socket operation).
EDIT: More information:I have a pool of Handler Threads which is initiated in onCreate (No problem when I'm launching my app at the first time).
Each handler runnable loop is wrapped with an
if (shouldRun) {
//body
}
else {
close();
}
statement.
the close method remove all pending messages and runnables and post a message to the handler that will cause him to call its looper.quit()
.This way, if the current handler thread is blocked by IO operation, only once it will finish it he will quit().
- You must have some inconsistency there, otherwise your App wouldn't crash. Are you sure that a HandlerThread which is not running is really the reason? Don't you create HandlerThread objects when your Activity is created?
- If your HandlerThreads are waiting on I/O operations, I would consider to try and interrupt them. Simply removing callbacks and messages and asking the Looper to quit, even sending temrination messages to the Handler, will do nothing. The HandlerThread object will still be around until Android kills the process (which may or may not occur). This means that "your app will collect zombie HandlerThread objects" which are probably unreachable. Unless, of course, you can send those HandlerThreads a termination message which arrives on the channel they block on.
- It would be much better to re-use the HandlerThread objects. A Service might be the right model to do this.
- Also, if Threads survive your Activity, you need to inform them that their communication peer (your Activity) is gone. Otherwise, they may refer to something which is already gone.
这篇关于销毁活动时,是否应该手动关闭由我的应用程序创建的HandlerThreads?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!