Running Parallel Asynctask

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



Leave a reply



Submit