0

i'm working on an android torrent app and I need to be able to download multiple torrents at the same time. i've seen other application doing it. I am able to download over 10 torrents at the same time on ttorrent android app. I'm currently using a service with async tasks, but i can't have more than 5 of them running at the same time (Running multiple AsyncTasks at the same time -- not possible?). Any idea on how this could be done?

2 Answers 2

0

All AsyncTasks are controlled internally by a shared (static) ThreadPoolExecutor and a LinkedBlockingQueue. When you call execute on an AsyncTask, the ThreadPoolExecutor will execute it when it is ready some time in the future.

The 'when am I ready?' behaviour of a ThreadPoolExecutor is controlled by two parameters, the core pool size and the maximum pool size. If there are less than core pool size threads currently active and a new job comes in, the executor will create a new thread and execute it immediately. If there are at least core pool size threads running, it will try to queue the job and wait until there is an idle thread available (i.e. until another job is completed). If it is not possible to queue the job (the queue can have a max capacity), it will create a new thread (upto maximum pool size threads) for the jobs to run in. Non-core idle threads can eventually be decommissioned according to a keep-alive timeout parameter.

Before Android 1.6, the core pool size was 1 and the maximum pool size was 10. Since Android 1.6, the core pore size is 5, and the maximum pool size is 128. The size of the queue is 10 in both cases. The keep-alive timeout was 10 seconds before 2.3, and 1 second since then.

With all of this in mind, it now becomes clear why the AsyncTask will only appear to execute 5/6 of your tasks. The 6th task is being queued up until one of the other tasks complete. This is a very good reason why you should not use AsyncTasks for long-running operations - it will prevent other AsyncTasks from ever running.

For completeness, if you repeated your exercise with more than 6 tasks (e.g. 30), you will see that more than 6 will enter doInBackground as the queue will become full and the executor is pushed to create more worker threads. If you kept with the long-running task, you should see that 20/30 become active, with 10 still in the queue.

2
  • Thanks for the great answer, you said "This is a very good reason why you should not use AsyncTasks for long-running operations". Do you know any other solutions that i can use for starting multiple (long-running) downloads?
    – user2382904
    Jun 12, 2013 at 12:38
  • Several methods exist, you can take a look at the MultiThreading methods developer.android.com/training/multiple-threads/… from API level 9 there is DownloadManager developer.android.com/reference/android/app/… . You can also implement your own version with Threads ThreadingPool and Locks...
    – An-droid
    Jun 12, 2013 at 12:49
-1

Why don't you use Android Download Manager. I don't know how it manages downloads but it seems like downloading lots of files at the same time.

Android Download Manager

2
  • torrent aren't http download. From the doc : "The download manager is a system service that handles long-running HTTP downloads."
    – user2382904
    Jun 11, 2013 at 15:09
  • Yeah right. Sorry I didn't come across anything about downloading torrents.
    – osayilgan
    Jun 11, 2013 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.