gpt4 book ai didi

java - 如何在多线程应用程序中衡量代码性能?

转载 作者:行者123 更新时间:2023-12-03 12:54:12 25 4
gpt4 key购买 nike

我目前正在从事一个项目,该项目不使用任何性能评估工具来计算代码执行所花费的时间。

相反,我在代码的开头和结尾使用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



我了解所有线程都彼此独立运行,并且我们可以使main 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在这种情况下不 sleep 。它只是在等待。
  • 在运行代码的末尾,让您的线程打印所花费的时间。这样,您可以看到每个线程的单独时间,并花费一些脑力周期自己计算总体执行时间。
  • 关于java - 如何在多线程应用程序中衡量代码性能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50790404/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com