Debarshi Dutta
Debarshi Dutta

Reputation: 442

When is the onPreExecute called on an AsyncTask running parallely or concurrently?

I am using Android HoneyComb.I need to execute some tasks parallely and I am using AsyncTask's public final AsyncTask executeOnExecutor (Executor exec, Params... params) method.In each separate thread I am computing some values and I need to store then in an ArrayList.I must then sort all the values in the arrayList and then display it in the UI.Now my question is if one of the thread gets completed earlier than the other then will it immediately call the onPostExecute method or onPostExecute method will be called after all the background threads have been completed?MY program implementation depends on what occurs here.

Upvotes: 1

Views: 1458

Answers (2)

Yahel
Yahel

Reputation: 8550

Even though using onexecuteExecutor will give you parallel execution, postexecute will be called by each asynctask as soon as it's finished.

But the asynctask will return itself so that you can know which is which if necessary and track their return order.

The doc

As for managing an arraylist from multiple threads, I think you should take a look at Vectors as they are synchronized with a fail-fast behavior. http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Vector.html

Upvotes: 1

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Now..... its this way...

AsyncTask synchronized the UI thread, and the Output of the Non-UI work on the Non-UI thread.

onPreExecute() is called on the UI thread, before the Non-UI work starts.

Then doInBackground() executes spinning the Non-UI thread, at this time onProgressUpdate(Progress...) keeps executing simultaneously.... but its highly unpredicatble about its execution.

After the doInBackground(), the onPostExecute() runs on the UI Thread.

See this link for further details:

http://developer.android.com/reference/android/os/AsyncTask.html

Upvotes: 3

Related Questions