我目前正在开发一个项目,其中没有使用任何性能测量工具来计算代码执行所需的时间。
相反,我使用 long <variableName> = System.nanoTime();
在代码的开始和结束处,减去它们即可得到完成执行所需的时间。
它工作得很好,直到我开始在多线程代码中使用它。
public class DataEntry {
public static void main(String[] args) {
long l = System.nanoTime();
/*
* Regular Multithreading code to generate 'n' number of threads to
* perform a certain task.
*/
System.out.println("Total Execution Time Is : "+((System.nanoTime()-l)/1000000.0)+" milli seconds");
}
}
我在这里面临的问题是我的 main()
线程在用户创建的线程完成其任务之前很久就完成了执行。
我的意思是,我得到类似这样的输出......
Total Execution Time Is : 9.600147 milli seconds
Thread#1 : Execution Over Bye Bye
Thread#2 : Execution Over Bye Bye
Thread#3 : Execution Over Bye Bye
Thread#4 : Execution Over Bye Bye
Thread#5 : Execution Over Bye Bye
All the child threads give their corresponding final o/p couple of seconds after
main()
has finished executing
我知道所有线程都彼此独立运行,并且我们可以使主线程 sleep() 直到所有子线程完成其执行。
我想知道是否有一种方法可以让我阻止任何线程相互等待,这样最后一个完成执行的线程负责返回我最终的执行时间。
下面是代码
DataEntry.java
public class DataEntry {
public static void main(String[] args) {
long l = System.nanoTime();
//STRING THAT WILL BE USED TO DOWNLOAD THE MULTIPLE FILES
String[] strDownload = {"https://unsplash.com/photos/n61ur6rT_F8/download?force=true",
"https://unsplash.com/photos/GLS3mY37RPo/download?force=true",
"https://unsplash.com/photos/v6asLq_dYzw/download?force=true",
"https://unsplash.com/photos/ePB2oGU8mb4/download?force=true",
"https://unsplash.com/photos/yEHQfGNKnZ4/download?force=true"};
//CREATE A FIXED NUMBER OF THREADS BASED ON THE LENGTH OF THE INPUT STRING ARRAY
for (int i = 0; i < strDownload.length; i++) {
Thread t = new Thread(new ThreadsGeneration(strDownload[i])); //PASSING THE i'th ELEMENT OF THE ARRAY AS THE PARAMETER FOR THE CONSTRUCTOR
t.start(); //CREATE THE THREAD
System.out.println(t.getName()+"\n");
}
System.out.println("Total Execution Time Is : "+((double)(System.nanoTime()-l)/1000000)+" milli seconds");
}
}
ThreadGeneration.java
public class ThreadsGeneration implements Runnable{
private static String string;
public ThreadsGeneration(String string) {
ThreadsGeneration.string = string;
}
@Override
public void run() {
System.out.println("Inside RUN");
DownloadManager.getData(string);
}
}
DownloadManager.java
public class DownloadManager {
public static void getData(String strDownload) {
String strDestination = "C:\\Users\\admin\\Desktop\\"+Math.random()+".jpg";
try {
printData(strDownload,strDestination);
} catch (Exception e) {
e.printStackTrace();
}
}
private static synchronized void printData(String strDownload, String strDestination) throws Exception {
URL url = new URL(strDownload);
ReadableByteChannel rbc = Channels.newChannel(url.openConnection().getInputStream());
FileOutputStream fos = new FileOutputStream(strDestination);
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);
fos.close();
rbc.close();
System.out.println("Everything done");
}
}
最佳答案
我能想到的两种方法:
通过调用
.join()
将所有线程与主线程连接起来,然后打印出所有线程连接所花费的时间。这样,主线程将轻松打印所花费的时间。在这种情况下,Main 不 hibernate 。它只是等待。
让你的线程在它们运行的代码结束时打印它们所花费的时间。这样您就可以看到每个线程的单独时间,并花一些时间自己计算总体执行时间。
关于java - 如何衡量多线程应用程序中的代码性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50790404/