Is it bad to have many HandlerThreads alive, doing nothing, just waiting?

282 Views Asked by At

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.

1

There are 1 best solutions below

2
On

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.