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:
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.
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.
Use the standard java. util. concurrent or Kotlin concurrency utilities instead.
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.
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.
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:
Now there are cons to all background threads no matter how beautifully illustrated they are, few include:
java.util.concurrent
has many classes to help in these scenariosAsyncTask
or the simpler Thread
a Service
or IntentService
would be an ideal option.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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With