Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this a prefect way to stop handlerthread?

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();
    }
like image 551
rex Avatar asked Jan 18 '12 09:01

rex


People also ask

How do I stop HandlerThread?

You can use this as a safe way to stop threads: if (safeThread!= null) { safeThread. quit(); safeThread = null; // Object is no more required. }

What is the difference between handler and HandlerThread?

The main difference between Handler and Thread is that a handler is a function or a method that is capable of performing a specific task while a thread is a small, lightweight execution unit within a process.

What is HandlerThread?

android.os.HandlerThread. A Thread that has a Looper . The Looper can then be used to create Handler s. Note that just like with a regular Thread , Thread.

How do you use handler threads?

How do I use HandlerThreads. There are 2 main ways that I found to use HandlerThreads. Create a new HandlerThread, create a new Handler using this HandlerThread and post your tasks on this handler. Extend HandlerThread inside your CustomHandlerThread, create a Handler to process your task.


2 Answers

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();
like image 137
Budius Avatar answered Sep 18 '22 02:09

Budius


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.

like image 23
SharkAlley Avatar answered Sep 22 '22 02:09

SharkAlley