2

I use Handler in conjunction with HandlerThread all over in my Android project. I use most of them in Services. There, my Handlers backed by an individual HandlerThread with low priority get created in onCreated() and stopped in onDestroy().

Many of them just wait the whole time. E.g. they process stuff for a few seconds each five minutes.

In total my app has about 20 threads (half of them are HandlerThreads). So, is that an performance issue to have so many threads open? In Is it bad to have threads waiting in java? I learnt, that it should be correct. I just want to check, if that applies to HandlerThreadalso.

Community
  • 1
  • 1
OneWorld
  • 17,512
  • 21
  • 86
  • 136

1 Answers1

1

About 20? Probably not too bad. It may slightly decrease the performance of the kernel's scheduler, but so long as you don't hit an OS limit and aren't polling, idle threads don't take CPU. They can take memory though, so make sure not to hold on to any references you don't absolutely need on those threads.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • I just use the method `postMessage(Runnable runnable)` of the `Handler` that basically acts as a Queue. I don't extend `Handler` or `HandlerThread`. So, I never reference something in the thread. – OneWorld Feb 03 '16 at 22:56
  • Then it shouldn't be too bad perf wise. Although transition them to IntentServices may make more sense from an architecture perspective, if all the services do is pass messages to a HandlerThread. – Gabe Sechan Feb 03 '16 at 23:00