At a certain point of my AsyncTask, after some validations have been done, I need to spawn off another thread to do some other work. So I'd like two background threads at this point, each doing it's own thing (approx 2-3 seconds to execute each). The idea is to maximize performance on dual core processors like Atrix.
Is it acceptable to create another asynctask & execute it from the first one? Can anyone suggest a better way of doing this?
Thanks!
EDIT: I'm wondering what publishProgress() from the second task would even do... since it was not started from an Activity?
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.
Methods of AsyncTaskwe can directly comminicate background operation using on doInBackground() but for the best practice, we should call all asyncTask methods . doInBackground(Params) − In this method we have to do background operation on background thread.
According to Android doco you must run an AsyncTask from a UI thread, but in reality it depends on who runs this line in AsyncTask.
Is it acceptable to create another asynctask & execute it from the first one?
Yes, but only inside onProgressUpdate()
or onPostExecute()
since these methods runs on the UI thread. Therefore, start the second AsyncTask
on the UI thread by choosing one of the two methods listed above.
I'm wondering what publishProgress() from the second task would even do... since it was not started from an Activity?
It does exactly the same thing, since you are starting it from the UI thread.
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