简体   繁体   English

如何在Android中启动多个下载

[英]how to start multiple downloads in android

i'm working on an android torrent app and I need to be able to download multiple torrents at the same time. 我正在使用android洪流应用程序,我需要能够同时下载多个洪流。 i've seen other application doing it. 我看过其他应用程序正在做。 I am able to download over 10 torrents at the same time on ttorrent android app. 我可以同时在ttorrent android应用上下载10个以上的torrent。 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? ). 我目前正在使用带有异步任务的服务,但不能同时运行5个以上的服务(同时运行多个AsyncTasks-不可能吗? )。 Any idea on how this could be done? 关于如何做到这一点的任何想法?

All AsyncTasks are controlled internally by a shared (static) ThreadPoolExecutor and a LinkedBlockingQueue. 所有AsyncTasks均由共享(静态)ThreadPoolExecutor和LinkedBlockingQueue内部控制。 When you call execute on an AsyncTask, the ThreadPoolExecutor will execute it when it is ready some time in the future. 当您在AsyncTask上调用execute时,ThreadPoolExecutor将在将来准备就绪时执行它。

The 'when am I ready?' “我什么时候准备好了?” behaviour of a ThreadPoolExecutor is controlled by two parameters, the core pool size and the maximum pool size. ThreadPoolExecutor的行为由两个参数控制,即核心池大小和最大池大小。 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 (ie 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. 在Android 1.6之前,核心池大小为1,最大池大小为10。从Android 1.6开始,核心孔径为5,最大池大小为128。在两种情况下,队列的大小均为10。 The keep-alive timeout was 10 seconds before 2.3, and 1 second since then. 保持活动超时是2.3之前的10秒,此后是1秒。

With all of this in mind, it now becomes clear why the AsyncTask will only appear to execute 5/6 of your tasks. 考虑到所有这些,现在可以清楚地知道为什么AsyncTask只会执行5/6的任务。 The 6th task is being queued up until one of the other tasks complete. 第6个任务正在排队,直到其他任务之一完成。 This is a very good reason why you should not use AsyncTasks for long-running operations - it will prevent other AsyncTasks from ever running. 这是一个很好的理由,为什么您不应该将AsyncTasks用于长时间运行的操作-这将阻止其他AsyncTasks永远运行。

For completeness, if you repeated your exercise with more than 6 tasks (eg 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. 为了完整起见,如果您重复执行超过6个任务(例如30个)的练习,则将看到doInBackground中有6个以上的任务将输入doInBackground,因为队列将变满并且执行程序被推入以创建更多工作线程。 If you kept with the long-running task, you should see that 20/30 become active, with 10 still in the queue. 如果继续执行长期运行的任务,则应该看到20/30处于活动状态,而10/30仍在队列中。

Why don't you use Android Download Manager. 您为什么不使用Android下载管理器。 I don't know how it manages downloads but it seems like downloading lots of files at the same time. 我不知道它如何管理下载,但是好像同时下载了许多文件。

Android Download Manager Android下载管理器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM