为什么使用线程不能加快我的程序? Java
Why the use of Threads doesn't speed up my program? Java
我对多重处理还很陌生,我想知道在多个文件的下载中使用它们有多方便。
基本上,我有一个应用程序(可以完美地从 URL 下载文件(图像、视频...),我想加快下载速度(现在它们是顺序的),将它们拆分到多个线程上。所以我创建了一个 class "PrimeThread" 覆盖线程 class 的 运行 方法,并且 运行 在主线程中为每次下载添加一个 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();
}
这是我在线程 class 中编写的代码:
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();
}
}
}
使用Thread.start代替Thread.run
澄清一下:你执行了方法PrimeThread.run,这只是一个普通的方法,是同步执行的。这意味着,在循环中,下一个 "run" 运行 仅在前一个完成之后。您可以安全地删除 "extends Thread" 并且您的程序将编译并且 运行 就好了。
线程class真正的"magic"在方法"start"中。它由 JVM 以特殊方式处理。它在 OS 级别创建一个线程并开始执行您放入 "run".
的任何代码段
顺便说一下,从 class 线程扩展在某种程度上不是一个好习惯。相反,您应该在 class 中定义要 运行 的代码,它实现 java.lang.Runnable 并使用 Thread 构造函数 new Thread(运行nable).
主循环:
for(int e = 0;e<videos.size();e++) //for every video i create a new thread
{
Thread thread= new Thread(new PrimeRunnable(downloader,path1,path2));
thread.start();
}
可运行:
class PrimeRunnable implements Runnable {
HttpDownloadUtility scaricatore; //instance of the "downloader" class
String path1, path2;
PrimeRunnable(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();
}
}
}
我对多重处理还很陌生,我想知道在多个文件的下载中使用它们有多方便。 基本上,我有一个应用程序(可以完美地从 URL 下载文件(图像、视频...),我想加快下载速度(现在它们是顺序的),将它们拆分到多个线程上。所以我创建了一个 class "PrimeThread" 覆盖线程 class 的 运行 方法,并且 运行 在主线程中为每次下载添加一个 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();
}
这是我在线程 class 中编写的代码:
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();
}
}
}
使用Thread.start代替Thread.run
澄清一下:你执行了方法PrimeThread.run,这只是一个普通的方法,是同步执行的。这意味着,在循环中,下一个 "run" 运行 仅在前一个完成之后。您可以安全地删除 "extends Thread" 并且您的程序将编译并且 运行 就好了。
线程class真正的"magic"在方法"start"中。它由 JVM 以特殊方式处理。它在 OS 级别创建一个线程并开始执行您放入 "run".
的任何代码段顺便说一下,从 class 线程扩展在某种程度上不是一个好习惯。相反,您应该在 class 中定义要 运行 的代码,它实现 java.lang.Runnable 并使用 Thread 构造函数 new Thread(运行nable).
主循环:
for(int e = 0;e<videos.size();e++) //for every video i create a new thread
{
Thread thread= new Thread(new PrimeRunnable(downloader,path1,path2));
thread.start();
}
可运行:
class PrimeRunnable implements Runnable {
HttpDownloadUtility scaricatore; //instance of the "downloader" class
String path1, path2;
PrimeRunnable(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();
}
}
}