0

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.

Manasi
  • 348
  • 1
  • 4
  • 14

3 Answers3

1

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

Community
  • 1
  • 1
0

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.

0101100101
  • 5,786
  • 6
  • 31
  • 55
  • after retriving information I set this information to a listView. All asyncTasks be setting retrived data to the same list will it create any problem – Manasi Sep 02 '14 at 09:45
  • The list implementation has to support concurrency, or you have to synchronize the writing to the list. Since the only existing concurrent list implementation is just awful, in my opinion, check [Collections.synchronizedList()](http://stackoverflow.com/a/6916461/2964379); Or [use something other than a list](http://stackoverflow.com/a/8203913/2964379). – 0101100101 Sep 02 '14 at 09:50
0

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.

Thread Pool

Executor

Thread.Executor Example

RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84