-1

In my application, there are multiple asynctasks. Please let me know why doInBackground of an asynctask sometimes does not getting called. Its onPreExecute method gets called. Is there any issue because of multiple asynctasks or something else?

/* ASync class for test table  */
    public class TestAsynch extends AsyncTask<String, Void, String>{
         protected void onPreExecute() {
         super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        String status = null;
        String result1=API_Manager.getInstance().sendTestData(userName);
        try {
            if(result1 != null) {
             // save in db
            }
        }
        }
        catch( Exception e) {
            e.printStackTrace();
            }
        return status;
    }
    @Override
    protected void onPostExecute(String status) {
    }
}
Anjana
  • 759
  • 2
  • 12
  • 33

3 Answers3

1

If your project has multiple asynctasks you must check that there is a limit of asynctasks that can be executed. When you create a new AsyncTask it will be added on a Pool and will be execute only when is possible. Check this answer: Multitasking on android And the docs: ThreadPoolExecutor

Here is an example on how properly handle multiple AsyncTasks AsyncTaskManager

Community
  • 1
  • 1
ikkarion
  • 178
  • 4
  • 17
  • Really Helpful. Can you please provide me an example how I can implement this, means how i can limit calling asynctasks. – Anjana Jun 10 '16 at 13:29
  • If you really need too many AsyncTaks, then you need to change the pool size. Here is an example: [AsyncTaskManager](https://developer.android.com/training/multiple-threads/create-threadpool.html?hl=pt-br) – ikkarion Jun 10 '16 at 13:40
0

OnPreExecute() gets called on the UI thread and doInBackground() is called on the background thread.

There is one dedicated background thread for the async task. This behaviour can be changed if you want to.

http://android-er.blogspot.in/2014/04/run-multi-asynctask-as-same-time.html

Now, say you have multiple instances of async task and I'm assuming you are calling execute() to run the async tasks. This will trigger all the preExecute immediately since UI thread is free but for the doInBackground it will triggered one by one. Hence it may take some time for the next async task to start.

Arpit Ratan
  • 2,976
  • 1
  • 12
  • 20
0

doInBackground should run on a loop using a Boolean to check before execution. Before your Task is being executed, set a global boolean (may be true/false) depends on which you prefer and values add on thread should call runOnUiThread.

startExect = true;
new TestAsynch().execute();

then change this

public class TestAsynch extends AsyncTask<String, Void, String>{
     protected void onPreExecute() {
     super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
    String status = null;
    String result1=API_Manager.getInstance().sendTestData(userName);
    try {
        if(result1 != null) {
         // save in db
        }
    }
    }
    catch( Exception e) {
        e.printStackTrace();
        }
    return status;
}
@Override
protected void onPostExecute(String status) {
}
}

to this

public class TestAsynch extends AsyncTask<String, Void, String> {

    String result1 = null;

    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... params) {
        String status = null;
        result1=API_Manager.getInstance().sendTestData(userName);

        while (startExecute) {
            Thread exe = new Thread(new Runnable() {
                @Override
                public void run() {

                    try {
                        Thread.sleep(5);
                    }
                    catch( Exception e) {
                        e.printStackTrace();
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if(result1 != null) {
                                // save in db
                            }   
                        }
                    });

                }
            }); exe.start();

        }

        return status;
    }

    @Override
    protected void onPostExecute(String status) {
    }
}
Franklyn
  • 325
  • 2
  • 8