我正在使用附加到 Android HandlerThread 的 Looper执行长时间运行的异步任务:
HandlerThread handlerThread = new HandlerThread("handler-thread");
handlerThread.start()
Handler myHandler = new Handler(callbacks, handlerThread.getLooper());
// Ready to handle messages in callbacks
当我完成工作线程后,我调用handlerThread.getLooper().quit()
来停止它的Looper。假设该线程没有执行其他操作,HandlerThread 实例在其 Looper 被 quit() 后是否终止(退出、死亡)?
最佳答案
Does the HandlerThread instance terminate (exit, die) after its Looper is quit(), assuming that that thread is performing no other execution?
Looper退出后,如果run()中没有其他处理要执行,则工作线程的run()方法返回。此时,handlerThread.isAlive()
返回 false,handlerThread.getState()
返回 TERMINATED 。
请注意,多次启动线程是不合法的,如 Thread API doc 中所述。 .
关于java - 清理已停止的 Looper 的线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21446976/