Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performing Background Tasks – alternative to AsyncTask?

I want to have various asynchronous threads in app like around 5-10 threads for background tasks which can be long running (like streaming) and I am also updating the UI to post some results if necessary.

From what I have heard that AsyncTask has problems with:

  • Long running tasks,
  • Having poorly being tied to Activity life cycle,
  • Device orientation problems, and
  • Memory leaks and so on.

So, I am looking an alternative (possibly without using any third party libraries) which doesn't have these above problems.

Should I be better off with using Simple Java Threads? I don't mind using them given that they won't give any problems that are with AsynTask.

like image 827
user963241 Avatar asked Feb 01 '17 04:02

user963241


People also ask

What can I use instead of AsyncTask?

Alternative 1: Using Executor and Handler The executor will help in performing any task in the background and the handler will help to make UI changes.

What can I use instead of AsyncTask in Java Android?

Use the standard java. util. concurrent or Kotlin concurrency utilities instead.

How can we overcome the AsyncTask limitations?

There are two simple solutions that cover most situations: Either check AsyncTask. isCancelled() on a regular basis during your long-running operation, or keep your Thread interruptible. Either way, when you call AsyncTask. cancel() these methods should prevent your operation from running longer than necessary.

What is the difference between AsyncTask and AsyncTaskLoader?

AsyncTask will be re-executed as background thread again, and previous background thread processing was just be redundant and zombie. AsyncTaskLoader will be just re-used basing on Loader ID that registered in Loader Manager before, so avoid re-executing network transaction.


2 Answers

In most scenarios AsyncTask should suffice the requirement. However there are scenarios where AsyncTask can't be used. ie AsyncTask manages a thread pool, from which it pulls the threads to be used by task instances. Now the thread pools assume that they'll get their threads back after a reasonable period of time. So in a scenario where you do not know how long you'll need the thread you can't use an AsyncTask. And as of Android 4.4 , the size of thread pool can grow only to : (no of CPU cores * 2) + 1. So on a dual core processor, the maximum number of threads you can create is limited to 5.

So, I am looking an alternative (possibly without using any third party libraries) which doesn't have these above problems.

Coming to the alternatives to AsyncTask, these are the available options:

  • Handler
  • Runnable

Now there are cons to all background threads no matter how beautifully illustrated they are, few include:

  • The possibility of user's interaction while the background thread is processing. If the work that the background thread performing is altered, you'd need to communicate it back to the background thread. java.util.concurrent has many classes to help in these scenarios
  • The possibility of the process itself being killed while the thread is performing tasks. So in these cases instead of using an AsyncTask or the simpler Thread a Service or IntentService would be an ideal option.
  • The possibility of an error occuring inside the background thread, such as retrieving data from server while connectivity is lost, you'd need to manually shut down the background thread.

In short: Whichever option you choose, you'd need to manually handle all corner cases for the efficient and splendid working of the app.

PS: [Citation] : The busy coder's guide to Android development v5.8 by @Commonsware which is released under the Creative Commons Attribution Non-Commercial Share Alike 4.0 License

like image 156
OBX Avatar answered Oct 11 '22 12:10

OBX


  • Long running tasks,
  • Having poorly being tied to Activity life cycle,
  • Device orientation problems, and
  • Memory leaks and so on.

Simple java threads are not going to solve any of these problems. Specially, memory leaks.

If you just want to load data in background, you can think about Loaders. They cache the data application wide and fit nicely with activity/fragment life cycle.

Alternately, you can go through this article to know about services (if you don't know already) and see if they are suitable in your scenario.

like image 21
mallaudin Avatar answered Oct 11 '22 11:10

mallaudin