The normal way we do AsyncTask in Android is, from Android API:
private class DoIntenseTask extends AsyncTask<Object, Object, Void> { protected Void doInBackground(Object... params) { for (Object param : params) { Object rtnObj = doIntenseJob(param); publishProgress(rtnObj); } return null; } protected void onProgressUpdate(Object... progress) { for (Object rtnObj : progress) { updateActivityUI(rtnObj); } } }
My intense tasks are loosely coupled and the execution order does not matter, by doing this way, a single thread is allocated to run a list of intense tasks. personally I think this is a sort of halfway solution. Yes, the intense job is not running in UI thread anymore, but still need execute one by one (in many cases, we are facing a list of intense job, I think this is also why the methods in AsyncTask are multi-parameterized). Google should make the API more reusable to solve different kind of scenario.
What I really like to have is run a number of doIntenseJob() in parallel managed by a threadpool (e.g. poolSize = 5). Looks like google do give a solution by AsyncTask.executeOnExecutor() but unfortunately only available since API level 11. I am developing app on mobile and wonder if there is a workaround that I can achieve the same behavior under API level 11.
Thanks in advance
Y
This class was deprecated in API level 30. AsyncTask was intended to enable proper and easy use of the UI thread. However, the most common use case was for integrating into UI, and that would cause Context leaks, missed callbacks, or crashes on configuration changes.
Android AsyncTask going to do background operation on background thread and update on main thread. In android we cant directly touch background thread to main thread in android development. asynctask help us to make communication between background thread to main thread.
In Android, AsyncTask (Asynchronous Task) allows us to run the instruction in the background and then synchronize again with our main thread. This class will override at least one method i.e doInBackground(Params) and most often will override second method onPostExecute(Result).
AsynTaskExample mAsyncTask = new AsyncTaskExample(); mAsyncTask. cancel(true);
If your build target is set to API Level 11 or higher, and you want to specifically use parallel tasks, you will want to start stating that explicitly in your code, akin to:
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) { myTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void[]) null); } else { myTask.execute((Void) null); }
http://commonsware.com/blog/2012/04/20/asynctask-threading-regression-confirmed.html
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