Android 中的多线程下载

发布于 2024-11-28 08:19:01 字数 787 浏览 0 评论 0原文

我已经测试了一些关于如何加速多次下载的信息。

此表显示了使用单个线程或每次下载的新线程通过不同网络下载(使用 Samsung Galaxy S1)2 到 10 个图像的速度。

        Multithread     SingleThread    
Images  Wifi    3g      Wifi    3g
   2    1,1     6,6     2,0     6,7
   4    1,7     8,0     5,2     9,6
   6    2,1     6,1     7,5     15,5
   8    2,3     7,1     10,2    20,0
   10   2,3     12,5    13,5    26,7

这些结果表明,多线程下载可以大大加快任务速度。

然后我尝试了相同的代码,但在 HTC Wildfire 上,当下载的线程数量很高时,UI 线程的运行流畅度要低得多。

我调整了代码以启动低优先级的每个线程:

Thread t= new Thread{
       public void run(){
          download(image[i]);
       }
t.setPriority(Thread.MIN_PRIORITY)
t.start

但问题仍然存在。有没有办法让这些线程不会对 UI 线程产生太大影响?如果不是,我如何检测线程数并将其调整为运行应用程序的设备?

谢谢

注意:十张图像的大小不同,因此没有意义按列阅读表格

I've tested a bit about how to speed up multiple downloads.

This is a table that shows how fast download (using a Samsung Galaxy S1) from 2 to 10 images over different networks using a single thread or a new thread for each download.

        Multithread     SingleThread    
Images  Wifi    3g      Wifi    3g
   2    1,1     6,6     2,0     6,7
   4    1,7     8,0     5,2     9,6
   6    2,1     6,1     7,5     15,5
   8    2,3     7,1     10,2    20,0
   10   2,3     12,5    13,5    26,7

With those results shows that multithreading the download accelerates a lot the task.

Then I tried the same code but with a HTC Wildfire, the UI thread run much less fluid when the number of threads downloading is high.

I addapted my code for launching each Thread with low priority:

Thread t= new Thread{
       public void run(){
          download(image[i]);
       }
t.setPriority(Thread.MIN_PRIORITY)
t.start

But the problem persists. Is there a way of make those threads don't affect the UI thread so much? If not how can I detect and adapt the number of threads to the device thats running the app?

Thanks

Note:The ten images have different sizes so have no sense to read the table by columns

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

靑春怀旧 2024-12-05 08:19:01

在三星手机中,由于某种原因,线程与 UI 线程竞争资源。尝试将线程优先级设置为后台...这将确保线程不与 UI 线程竞争,好处是它可以在所有设备上工作。如果你查看 asynctask 的源代码,他们将线程优先级设置为上述值

In Samsung phones for some reason the threads compete with UI thread for resources. Try setting thread priority to background...this will ensure the thread does not compete with UI thread and the good thing is it will work across all devices..If you look into the source code of asynctask, they set the thread priority to the above mentioned value

江南月 2024-12-05 08:19:01

对于下载,请始终使用 AsyncTask,它正是这样做的(并且会让如果需要,您可以轻松发布下载完成情况)。另外,就 UI 感觉不流畅而言,除了线程之外,还有很多因素会导致这种情况。使用 DDMS 运行您的代码,并验证如果您的测试发生其他任何情况,您不会导致 GC 不断触发。

根据我的经验,在 Android 上设置线程优先级会产生好坏参半的结果,即通过将其设置得较低来获得更好的性能。异步任务似乎更一致地执行此操作。

For downloading, always use AsyncTask, which does exactly this (and will let you easily publish download completion, if desired). Also, in terms of the UI feeling not feeling fluid, many things can contribute to that, other than threading. Run your code with DDMS and verify you can't causing the GC to fire constantly if your test has anything else going on.

In my experience, setting the thread priority has had mixed results on Android in terms of getting better performance by setting it lower. Async task seems to do this more consistently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文