Home » Android » android – Multiple Async Task Hanging the App

android – Multiple Async Task Hanging the App

Posted by: admin May 14, 2020 Leave a comment


In my android App, I use multiple AsyncTask using THREAD_POOL_EXECUTOR which makes the tasks run in parallel. Sometime the app hangs. Below is the code I use.

  1. Could you please let me know how to fine tune so as to avoid any hanging issue ?
  2. How to find the point in which app is hanging ?

    new fetchInitialCoinsParallel().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
    prefCoinList = getPrefCoin();
        for(int i=0;i<prefCoinList.size();i++){
            new fetchAltCoinsParallel().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, url);
    public class fetchAltCoinsParallel extends AsyncTask<String, String, String> {
    protected void onPreExecute() {
    protected String doInBackground(String... params) {
        try {
            InputStream is = getDataFromURL(params[0]);
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                try {
                } catch (IOException e) {
        } catch (IOException e) {
        return null;
    protected void onPostExecute(String params) {
    protected void onProgressUpdate(String... progress) {


Thread details
enter image description here

How to&Answers:

Instead of using Async Tasks, i recommend using RXJAVA for this purpose.

Here is the disadvantage given for Async Task:

Using RxJava will solve this problem, here is an perfect blog which can solve your problem of Multi Threading using RxJava.


(Read the second half) Both flatmap and parallel operators would be useful in your case.


Check the AsyncTaskLoader concept. This feature is supported by Android community introduced in API level 11 along with Honeycomb features.

AsyncTaskLoader solved the lot of limitations & workaround solutions of the AsyncTask.java

Official : https://developer.android.com/reference/android/content/AsyncTaskLoader.html

Good Sample: https://medium.com/google-developers/making-loading-data-on-android-lifecycle-aware-897e12760832

public class JsonAsyncTaskLoader extends AsyncTaskLoader<List<String>> {
    // You probably have something more complicated
    // than just a String. Roll with me
    private List<String> mData;

    public JsonAsyncTaskLoader(Context context) {

    protected void onStartLoading() {
        if (mData != null) {
            // Use cached data
        } else {
            // We have no data, so kick off loading it

    public List<String> loadInBackground() {
        // This is on a background thread
        // Good to know: the Context returned by getContext()
        // is the application context
        File jsonFile = new File(
                getContext().getFilesDir(), "downloaded.json");
        List<String> data = new ArrayList<>();
        // Parse the JSON using the library of your choice
        // Check isLoadInBackgroundCanceled() to cancel out early
        return data;

    public void deliverResult(List<String> data) {
        // We’ll save the data for later retrieval
        mData = data;
        // We can do any pre-processing we want here
        // Just remember this is on the UI thread so nothing lengthy!