HandlerThread vs Executor - When is another approach to another?

I'm just wondering if there are times when I have to select Executor over HandlerThread . Are there times when one is superior to the other, or should I just stick to HandlerThread ? In my case, I am currently listening to ServerSocket for connections and process each request in a separate thread created by Executor . Despite the fact that I gave a concrete example, I am really looking for cases when one of them is more appropriate than the other. However, I welcome comments about my design.

+27
android concurrency
Sep 18 2018-11-18T00:
source share
2 answers

The Executor class is more powerful and can use a thread pool, while each handler refers to a single thread. The Contractor allows you to complete all scheduled tasks and cancel them if you want. The handler, on the other hand, will not answer simple questions, for example, how many tasks are waiting or will give me a link to all waiting tasks. I believe that one of the reasons that Handler is more limited is because Android gives you access to the main handler that it uses for the user interface, and you can really ruin the OS if you start to cancel OS tasks.

In general, if you need a pool of threads or a lot of energy, use the Contractor. If you just need a good background thread to run one task at a time, use Handler. As an example, when I want to query my database, I just want one query to appear at a time, and I don't want to generate ANR, so I use a handler running in the background thread to run my queries.

I believe that your choice of executor sounds correct, since you want to process several incoming requests at the same time, and the processor can do only one at a time.

UPDATE: how to create a handler that works in the background thread:

In your constructor or onCreate write the following, it is obvious that you can set the priority as you like:

 public class MyClass { private Handler mBgHandler; public MyClass() { HandlerThread bgThread = new HandlerThread("My-Background-Handler"); bgThread.start(); mBgHandler = new Handler(bgThread.getLooper()); } } 

UPDATE: don't forget to leave () or quitSafely () your HandlerThread when you are done with it, otherwise it will remain waiting forever

+44
Sep 19 2018-11-11T00:
source share

I will not follow the sample code in satur9nine's answer as of 2011-Dec-22.

Thread.MIN_PRIOROTY maps to android.os.Process.THREAD_PRIORITY_LOWEST. Quote :

Lowest thread priority available. Only for those who really really don't want to run if something else happens.

I would at least use android.os.Process.THREAD_PRIORITY_BACKGROUND, for example:

 HandlerThread bgThread = new HandlerThread("handler name"); Process.setThreadPriority(bgThread.getThreadId(), Process.THREAD_PRIORITY_BACKGROUND); bgThread.start(); mBgHandler = new Handler(bgThread.getLooper()); 

This prioritizes the default Android background image for the stream.

Currently, the priority threads Process.THREAD_PRIORITY_BACKGROUND and below share an artificially limited amount of processor time using the Linux group, see, for example, here . If the background task is not just waiting for I / O, but also performing real calculations, I would consider increasing my priority with android.os.Process.THREAD_PRIORITY_MORE_FAVORABLE, which (currently) moves it from the background group, but still not poses threats to the user interface and in real time.

Update:. Satur9nine's answer was reversed in 2013-Jan-08 so as not to set the lowest priority. HandlerThread now implies android.os.Process.THREAD_PRIORITY_BACKGROUND priority. This means that now it gets the priority of the background task by default, but it is still limited by consuming an artificial maximum of 10% of the processorโ€™s time, along with all other background tasks that may exist. If this is undesirable, use my code above, for example. from

 Process.setThreadPriority(bgThread.getThreadId(), Process.THREAD_PRIORITY_BACKGROUND + Process.THREAD_PRIORITY_MORE_FAVORABLE); 

to remove the background stream from the group.

+12
Jan 08 '13 at 12:18
source share



All Articles