running parallel AsyncTask
You will need to use a thread pool Executor
to execute AsyncTask
. Default implementation uses a serial executor running on a single thread
So create a ThreadPoolExecutor and then use
AsyncTask's executeOnExecutor instead of just execute method
Running async methods in parallel
Is there a better to run async methods in parallel, or are tasks a good approach?
Yes, the "best" approach is to utilize the Task.WhenAll
method. However, your second approach should have ran in parallel. I have created a .NET Fiddle, this should help shed some light. Your second approach should actually be running in parallel. My fiddle proves this!
Consider the following:
public Task<Thing[]> GetThingsAsync()
{
var first = GetExpensiveThingAsync();
var second = GetExpensiveThingAsync();
return Task.WhenAll(first, second);
}
Note
It is preferred to use the "Async" suffix, instead of GetThings
and GetExpensiveThing
- we should have GetThingsAsync
and GetExpensiveThingAsync
respectively - source.
Related Topics
How to Find MAC Address of an Android Device Programmatically
Custom Info Window Adapter with Custom Data in Map V2
How to Determine the Screen Width in Terms of Dp or Dip at Runtime in Android
How to Set Multiple Alarms Using Alarm Manager in Android
Java.Lang.Outofmemoryerror: Bitmap Size Exceeds Vm Budget
Custom Row in a Listpreference
Android: Wrap_Content Is Not Working with Listview
Check If Battery Optimization Is Enabled or Not for an App
How to Show Multiple Markers on Mapfragment in Google Map API V2
How to Get Current Flavor in Gradle
Need an Example About Recyclerview.Adapter.Notifyitemchanged(Int Position, Object Payload)
How to Turn Speaker On/Off Programmatically in Android 4.0
Adding an Onclicklistener to Listview (Android)
Check If Correct Google Play Service Available: "Unfortunately Application Has Stopped Working"