Is this a prefect way to stop handlerthread?

后端 未结 4 1970
你的背包
你的背包 2020-12-31 08:35

I always create two handler: one is wraping on main thread, another is wraping on a single thread.

Is this a best method to start and stop these in an Activity lifec

4条回答
  •  醉酒成梦
    2020-12-31 09:30

    I know it's a somewhat old question, but I stumble across it looking for the same answer and further researching I don't believe the current answer applies very well to HandlerThread (although absolutely correct for normal threads)

    HandlerThread have a builtin quit() and quitSafely(API18) methods to stop the thread. https://developer.android.com/reference/android/os/HandlerThread.html#quit()

    the difference between the two is only if the messages in queue will be processed before it stops or not.

    so to answer, it's as simple as:

    safeThread.quit();
    

提交回复
热议问题