【发布时间】:2018-11-09 10:37:37
【问题描述】:
我对多处理还很陌生,我想知道在多个文件的下载中使用它们有多方便。 基本上,我有一个应用程序(完全可以从 URL 下载文件(图像、视频......),我想加快下载速度(现在它们是连续的)将它们拆分到多个线程上。所以我创建了一个类“PrimeThread " 覆盖线程类的 run 方法并在每次下载时在 main 中运行一个 Thread 实例,但我没有注意到时间性能有任何加快。 这是我写的代码(主要):
for(int e = 0;e<videos.size();e++) //for every video i create a new thread
{
PrimeThread thread= new PrimeThread(downloader,path1,path2);
thread.run();
}
这是我在 Thread 类中编写的代码:
import java.io.IOException;
class PrimeThread extends Thread {
HttpDownloadUtility scaricatore; //instance of the "downloader" class
String path1, path2;
PrimeThread(HttpDownloadUtility scaricatore,String path1,String path2) {
this.scaricatore = scaricatore;
this.path1 = path1;
this.path2 = path2;
}
public void run() {
try {
scaricatore.downloadMedia(path1, path2); //method of the "downloader" class that takes 2 paths in input and downloads from the 1st and put the file downloaded in the 2nd path
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
【问题讨论】:
-
在您的情况下,您的实际带宽可能是瓶颈。顺序或并行,单位时间内可以传输的数据量是有限制的。
-
如果你的速度受限于网络,多线程根本无济于事。
标签: java multithreading url download threadpool