18

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 lifecycle?

HandlerThread safeThread = null;
Handler safeHandler = null;
Handler handler = null;


@Override
    public void onStart() {

if (safeThread == null) {
            safeThread = new HandlerThread("safeThread");
            safeThread.start();
            safeHandler = new Handler(safeThread.getLooper());
        } else if (safeThread.getState() == Thread.State.NEW) {
            safeThread.start();
            safeHandler = new Handler(safeThread.getLooper());
        } else if (safeThread.getState() == Thread.State.WAITING) {
            safeHandler = new Handler(safeThread.getLooper());
        } else if (safeThread.getState() == Thread.State.TERMINATED) {
            safeThread = null;
            safeThread = new HandlerThread("safeThread");
            safeThread.start();
            safeHandler = new Handler(safeThread.getLooper());
        }
}




protected void onStop() {
        Log.d("x", "onStop is executed");
        safeHandler = null;
        safeThread.quit();
        safeThread.interrupt();
        safeThread = null;
        super.onStop();
    }
Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
rex
  • 821
  • 3
  • 10
  • 21
  • safeThread.interrupt(); is best way to stop thread – Tofeeq Ahmad Jan 18 '12 at 09:22
  • yes , i know but i thinking about is this safe when i after interrupt and null this. – rex Jan 18 '12 at 09:37
  • 1
    i have given answer..one thing i would suggest you as you are new on stackoverflow.IF you want to appreciate someone help then give him a vote and if you think answer is best then mark it as answer..:) welcome and cheers – Tofeeq Ahmad Jan 18 '12 at 09:42

4 Answers4

25

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();
Budius
  • 39,391
  • 16
  • 102
  • 144
  • To add clarity: quit() is available in API 5 – Vyrx Jul 30 '15 at 13:43
  • 2
    What to do before api 18? – JohnyTex Feb 16 '16 at 11:14
  • 1
    Does quit() or quitSafely() stops Thread or only the Looper that thread has to handle messages? It does not explain if it ends thread or not [here](https://developer.android.com/reference/android/os/HandlerThread.html#quit()). – Thracian Oct 17 '17 at 15:01
  • @Thracian the looper is a for-loop cycle that runs in `HandlerThread#run()` method forever. This prevents the `run()` method from reaching the end, thus keeping the `Thread` alive. The `Thread` is terminated right after the looper is quit. – Max_Payne Apr 14 '23 at 08:36
17

If you start the thread like this:

HandlerThread thread = new HandlerThread("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.

SharkAlley
  • 11,399
  • 5
  • 51
  • 42
1

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.

Tofeeq Ahmad
  • 11,935
  • 4
  • 61
  • 87
0

I have been following this :

if (safeHandler!= null) {
     final Looper looper = safeHandler.getLooper();
     looper.quitSafely();
     safeHandler = null;
 }
iammrmehul
  • 730
  • 1
  • 14
  • 35