1

In my android application, I'm listening to a couple of system events (which don't get fired quite often) and after each event I post a task which is executed after a short delay (usually 1 minute) in the background. (after the task completes, pending some conditions, it can be posted again)

My approach was to instantiate a Singleton which has a HandlerThread and post my tasks to that handler thread. (See code snippet below) I understand This thread is now alive through out the life cycle of my application.
my question is:
Is there a performance hit for keeping a HandlerThread alive, even while it isn't doing any work?

public class Observer {

    private HandlerThread workerThread;
    private Handler mHandler;

    private Observer() {
        workerThread = new HandlerThread("observer-thread", Process.THREAD_PRIORITY_BACKGROUND);
        workerThread.start();
        mHandler = new Handler(workerThread.getLooper());
    }

    public static Observer getInstance() {
        if (mInstance == null) {
            synchronized (Observer.class) {
                if (mInstance == null) {
                    mInstance = new Observer();
                }
            }
        }

        return mInstance;
    }

    public void post(...) {
        ...
    }
}
guy.gc
  • 3,359
  • 2
  • 24
  • 39

0 Answers0