android - Necessary to quit a HandlerThread? -


my application makes use of handlerthread few operations shared across components need run on background thread. of time thread in wait state.

can leave handlerthread running (waiting) in application, sending messages whenever necessary, never quitting via handlerthread.getlooper().quit()? mean handlerthread continue exist in wait state after of application components have been destroyed.

initially seemed big no me—something not want do—but i'm not sure now. when android kills process, when needs free cpu time or memory, it'll end thread along ui thread. additionally, thread waiting, wont consuming cpu time. , beyond that, application makes use of many asynctasks, know utilize thread pool. understanding, asynctask utilizes threadpoolexecutor, not adhere application lifecycle callbacks (the threads in pool when not in use, sit waiting).

so question is, can use handlerthread across multiple application components, never (or rarely) quitting it, , leaving waiting when not in use, without suffering terrible ill effects?

my personal preference create new thread whenever there need , clean when it's done. way don't have problems multiple components trying use same thread @ same time , keep "clean ship". android has nice asynctask makes easy you.

that being said, see no reason why can't reuse handlerthread multiple components, provided regulate access thread , clean when activities destroyed. if understand this post correctly, thread may keep running if activities terminated because process may keep on running. solve this, can set thread daemon thread. daemon threads automatically destroyed when last non-daemon thread in application finished.

btw, alternatively might want consider using threadpoolexecutor


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -