Is This A Prefect Way To Stop Handlerthread?
Solution 1:
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();
Solution 2:
If you start the thread like this:
HandlerThread thread = newHandlerThread("MyHandlerThread");
thread.start();
The safest way to stop it would be:
thread.quitSafely();
quitSafely ensures that all pending messages are processed before the thread stops.
Note: My original answer included an additional call to thread.join, but I found the join call does not release reliably when used after quitSafely.
Solution 3:
You can use this as a safe way to stop threads:
if (safeThread!= null) {
safeThread.quit();
safeThread = null; // Object is no more required.
}
You can use safeThread.quitsafely
as well.
Solution 4:
I have been following this :
if (safeHandler!= null) {
finalLooperlooper= safeHandler.getLooper();
looper.quitSafely();
safeHandler = null;
}
Post a Comment for "Is This A Prefect Way To Stop Handlerthread?"