title: Short but frequent jobs: HandlerThread or ThreadPoolExecutor?
body: <p>First of all, I could not determine what the title should be, so if it's not specific enough, the question itself will be.</p>
  
  <p>We have an application that uses a foreground service and stays alive forever, and in this service, there are frequent database access jobs, network access jobs and some more, that needs to run on background threads. One job itself consumes a small amount of time, but the jobs themselves are frequent. Obviously, they need to run on worker threads, so I'm here to ask which design we should follow.</p>
  
  <p><a href="https://developer.android.com/reference/android/os/HandlerThread" rel="nofollow noreferrer"><code>HandlerThread</code></a> is a structure that creates a singular thread and uses a queue to execute tasks but always loops and waits for messages which consumes power, while <a href="https://developer.android.com/reference/java/util/concurrent/ThreadPoolExecutor" rel="nofollow noreferrer"><code>ThreadPoolExecutor</code></a> creates multiple threads for each job and deletes threads when the jobs are done, but because of too many threads there could be leaks, or out-of-memory even. The job count may be 5, or it may be 20, depending on how the user acts in a certain way. And, between 2 jobs, there can be a 5 second gap, or a day gap, totally depending on user. But, remember, the application stays alive forever and waits for these jobs to execute. </p>
  
  <p>So, for this specific occasion, which one is better to use? A thread pool executor or a handler thread? Any advice is appreciated, thanks.</p>