多线程下载的安卓 [英] Multithreading for download in Android
问题描述
我测试了一下关于如何加快多个下载。
I've tested a bit about how to speed up multiple downloads.
这是一个表格,显示了如何快速下载使用单线程或在每次下载一个新的线程为2至10的图像在不同网络(使用的是三星Galaxy S1)。
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.
然后我试图在同一code,但与HTC野火,UI线程运行得少津,当线程下载的数量是很高的。
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.
我addapted我的code发动每个线程以低优先级:
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
但问题仍然存在。有没有使这些线程不会影响到UI线程这么多的方法吗?如果没有我怎样才能检测和适应线程数到设备多数民众赞成在运行应用程序?
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?
感谢
注:十个图像有不同的大小等有没有道理读取表按列
Note:The ten images have different sizes so have no sense to read the table by columns
推荐答案
有关下载,请始终使用的 AsyncTask的,这正是这一点(和可以让你轻松地发布下载完成,如果需要的话)。此外,在UI方面感觉不舒服流畅,很多事情都有助于如此,除了线程。运行您的code与DDMS和验证你不能使GC火灾不断,如果你的测试有别的事情。
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.
在我的经验,设置线程优先级设置为较低的数值得到更好的性能方面已经对Android的结果好坏参半。异步任务似乎更加一致地做到这一点。
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.
这篇关于多线程下载的安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!