【发布时间】:2018-11-20 06:31:08
【问题描述】:
我目前正在做一个项目,我没有使用任何性能测量工具来计算代码执行所需的时间。
相反,我在代码的开头和结尾使用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
所有的 child 线程都给出了它们对应的 final o/p 在
main()完成执行后几秒钟
我知道所有线程彼此独立运行,我们可以让 main sleep() 直到所有 child 线程完成它们的执行。
我想知道是否有一种方法可以让我防止任何线程相互等待,以便完成其执行的最后一个线程负责将最终执行返回给我时间。
下面是代码
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);
}
}
下载管理器.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");
}
}
【问题讨论】:
-
你能在线程的启动和加入之外做计时吗?
-
简单的答案是通过创建一个扫描线程池的while循环来保持主线程直到结束,并且只有当所有线程都关闭时才返回时间。保持主线程存活并没有什么坏处。
-
也许您要测量的内容更具体一些。我不认为这是 100% 清除 atm
-
一种选择是使用
CountDownLatch等待 -
@user7 您能否详细说明一下,如果可能,请提供示例代码?
标签: java multithreading performance