Reputation: 348
In my app I have to download some JSON data from different JSON files. For this I ll use AsyncTask. I have 7 links for 7 JSON files. Which is correct way 1. to start an asyncTask from MainActivity and run a loop in it for 7 links 2. Or write an asyncTask that takes URL as parameter. And start this asyntask for each link.
Is starting multiple asyncTasks simultaneously same.
Upvotes: 0
Views: 80
Reputation: 10630
It is possible to run multiple async tasks parallely from HONEYCOMB version and also it is safe. So the way of implementing differs in honeycomb we have a concept called Thread Executor. Here is the example for Thread Executor.
Upvotes: 0
Reputation: 189
I think it is more safe to use Executor because AsyncTask is designed to be a helper class around Thread and Handler and does not constitute a generic threading framework. AsyncTasks should ideally be used for short operations (a few seconds at the most.) and server operations may take long time, If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent pacakge such as Executor, ThreadPoolExecutor and FutureTask.
Check this answer https://stackoverflow.com/a/9906890/1826581
Also Check this link http://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 1
Reputation: 5911
Definitely safe, works well in my app (6 tasks getting JSON in parallel). It's also much faster than doing it sequentially. You just have to make sure you start a new AsyncTask for each operation -- it's not possible to reuse an AsyncTask.
Upvotes: 0