Murphybro2
Murphybro2

Reputation: 2689

Android AsyncTask waiting/queued

I have a class extending an AsyncTask that sends messages to a WCF web service. Simple messages one by one will work fine, but if I send a message on a new thread that takes 30 seconds to complete, then midway through that I send a quick request it won't execute the AsyncTask until the long one has returned.

I thought the whole idea of AsyncTask was these two messages would run on different threads and therefore wouldn't stack?

Here is my code:

private class RunnableTask extends AsyncTask<RunnableObj, RunnableObj, RunnableObj> {
    @Override
    protected RunnableObj doInBackground(RunnableObj... params) {
        try {
            if (params[0].requestBody != (null)) {
                params[0].request.body(new JSONObject(params[0].requestBody));
            }

            params[0].request.asVoid();

            return params[0];
        }
        catch (Throwable e) {
            params[0].handler.onFailure(e);
            e.printStackTrace();
        }

        return null;
    }

    @Override
    protected void onPostExecute(RunnableObj runnableObj) {
        super.onPostExecute(runnableObj);

        runnableObj.handler.onSuccess();
    }
}

This is my AsyncTask above.

public void put(final String urlStr, final String requestBody, final HttpResponseHandler httpResponseHandler) {
    RunnableObj obj = new RunnableObj();
    obj.handler = httpResponseHandler;
    obj.request = webb.put(urlStr)
            .header(ServiceConstants.SessionTokenHeader, MyApplication.getSessionToken())
            .ensureSuccess();
    obj.requestBody = requestBody;

    new RunnableTask().execute(obj);
}

This is the method I use to call the Async.

As you can see in the method I use to call the service, I initialise a new instance of RunnableTask each time.

How it performs:

Please can someone help? Or at least tell me I'm doing something very stupid..

Upvotes: 1

Views: 2000

Answers (2)

Mobile Apps Expert
Mobile Apps Expert

Reputation: 1048

You can execute multiple AsyncTask by using executeOnExecutor

 if( Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ) {
    new MyAsyncTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    new MyAsyncTask().execute();
}

For more check the AsyncTask documentation

Upvotes: 6

NoChinDeluxe
NoChinDeluxe

Reputation: 3444

According to the AsyncTask Docs:

When first introduced, AsyncTasks were executed serially on a single background thread. Starting with DONUT, this was changed to a pool of threads allowing multiple tasks to operate in parallel. Starting with HONEYCOMB, tasks are executed on a single thread to avoid common application errors caused by parallel execution.

If you truly want parallel execution, you can invoke executeOnExecutor(java.util.concurrent.Executor, Object[]) with THREAD_POOL_EXECUTOR.

As you can see, AsyncTasks currently only operate on a single background thread, meaning multiple queued tasks will have to fire one after another. If you would like to execute concurrent tasks, you'll want to follow the instructions above.

Upvotes: 0

Related Questions