For the background task execution in the Android environment, 4 classes are available: HandlerThread , Thread , IntentService , AsyncTask . Using which class is effective for performing background tasks if tasks arise non-permanently?

The main task is to obtain information from the Internet. Due to the impermanence of the tasks, it is unclear what class to use for achieving the best efficiency.

HandlerThread is good for performing frequent requests, but for rare ones, a simple thread. Thread is good with rare requests, but with frequent - a drop in performance (object creation + reservation of necessary resources). AsyncTask similar to Thread. IntentService is good for all tasks, but performance problems may be due to the frequent launch of the service by the android system.

Who can that advise?

    1 answer 1

    You should rather be worried about whether you need the work to be completed to the end or not particularly important. Because if you absolutely need the thread to complete its work, then definitely - IntentService, if you do not care, you can use AsyncTask, believe me, it does not eat extra CPU time