1

I know AsyncTasks make things run on different threads. So after I log a user in should I grab their friends names, profile pic, etc. all using different asynctasks but simultaneously to minimize time?

bharv14
  • 493
  • 1
  • 6
  • 15
  • there is no way to really answer this. If you are getting all that data from the same place then it will defiantly be faster to only make one call. Then problems come with trying to return way too much data at once. Sometimes you need to gather all that data from different locations then you will find async will perform better. Sometimes you will find it is best to just load what is needed for initial view – Scott Selby Oct 15 '14 at 18:26
  • The only way to know for sure is by profiling it. – Anubian Noob Oct 15 '14 at 18:32
  • Not a single correct answer below. Short Answer: By default, using multiple AsyncTasks will not make your app run faster. That is because AsyncTasks are executed serially. You will need to change the default behavior using `executeOnExecutor` to see a benefit... and then yes, if you're pulling data from REST services, executing parallel requests will result in faster retrieval overall. – 323go Oct 15 '14 at 18:44
  • @bharv14 323go is also correct as already mentioned for execution in parallel with `asyncTask.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR)` to be a bit more precise. But again, don't do so unless you are dealing with quick operations. – Jay Snayder Oct 15 '14 at 19:10

3 Answers3

2

Yes and No

Using multiple threads doesn't necessarily mean that the job will be done faster.

The more threads you are running, the more context switching your gonna suffer.

If your gonna use normal thread i recommend you use a thread pool so things doesn't go out of hand.

Asynctasks use pools internally, and have 2 modes they can operate on, serial or parallel.

if you use execute() method it will run parallel on devices running API less than 13, and serially on ones running later APIs

To force parallel behavior after API 13 use executeOnExecutor()

For devices with multiple cores , go for the parallel behavior, ones with single core, many threads are bound to slow things down

elmorabea
  • 3,243
  • 1
  • 14
  • 20
  • 1
    +1 for "if you use execute() method it will run parallel on devices running API less than 13, and serially on ones running later APIs" – Alex Mar 01 '19 at 17:11
0

You should typically use only a minimal subset of threads based on your needs. You should typically only use AsyncTasks for components that will operate for short periods of time and need to interact with the UI components separately from a different task.

By default, the ThreadPoolExecutor will decide how serialized multiple AsyncTasks would be if you called upon them. Try to do small tasks in a single AsyncTask, and if you need longer running threads, then consider doing a service that runs in the background.

Jay Snayder
  • 4,298
  • 4
  • 27
  • 53
0

Yes you can if you want to store all your data in database and it is suppose to use on different actions.

But at a time max 5 to 6 asyncTask will run and if they are running and you want to execute some other asynctask on user action then that will be goes into queue and user might require more time to wiat in that case. for more details on multiple asynctask see this thread ..

This

I hope this all will clear your idea.

Community
  • 1
  • 1
khurram
  • 1,362
  • 13
  • 24