如何让循环停止
How to get loop to stop
我需要有关如何使此代码中的循环在加速因子达到特定值时停止执行的帮助或一些想法。这种方法的想法是不断 运行 不断增加的线程数量,并从结果中得出加速因子。四舍五入的加速因子是机器上有多少个内核。 运行 4 线程任务与 4 核机器上的 16 线程任务具有相同的加速因子。我希望不必手动将线程数设置为 运行。当加速因子稳定到一个值时,我希望程序终止。例如,如果加速因子已经稳定在 2,则无需 运行 测试 8、16 或 32 个线程。
4 核机器的示例输出:
测试的线程数:1
加速因子:1.0
测试的线程数:2
加速因子:1.8473736372646188
测试的线程数:4
加速因子:3.9416666666666669
测试的线程数:8
加速因子:3.9750993377483446
测试的线程数:16
加速因子:4.026086956521739
这台机器有:4 个内核
应用程序已完成执行。谢谢
private static void multiCoreTest() {
// A runnable for the threads
Counter task = new Counter(1500000000L);
// A variable to store the number of threads to run
int threadMultiplier = 1;
// A variable to hold the time it takes for a single thread to execute
double singleThreadTime = ThreadTest.runTime(1, task);
// Calculating speedup factor for a single thread task
double speedUp = (singleThreadTime * threadMultiplier) / (singleThreadTime);
// Printing the speed up factor of a single thread
System.out.println("Number of threads tested: " + threadMultiplier);
System.out.println("Speed up factor: " + speedUp);
// Testing multiple threads
while (threadMultiplier < 16) {
// Increasing the number of threads by a factor of two
threadMultiplier *= 2;
// A variable to hold the time it takes for multiple threads to
// execute
double multiThreadTime = ThreadTest.runTime(threadMultiplier, task);
// Calculating speedup factor for multiple thread tests
speedUp = (singleThreadTime * threadMultiplier) / (multiThreadTime);
// Message to the user
System.out.println("\n" + "Number of threads tested: "
+ threadMultiplier);
System.out.println("Speed up factor: " + speedUp);
}
// Print number of cores
System.out.println("\n" + "THIS MACHINE HAS: " + Math.round(speedUp)
+ " CORES");
System.out.println("\n"
+ "THE APPLICATION HAS COMPLETED EXECUTION. THANK YOU");
// Exiting the system
System.exit(0);
}
}
测试新加速是否与旧加速相同:
double oldSpeedUp = 0;
boolean found = false;
while(!found && threadMultiplier < 16) {
// ...
found = Math.round(speedUp) == Math.round(oldSpeedUp);
oldSpeedUp = speedUp;
}
附带说明一下,如果你想要核心数,你可以调用:
int cores = Runtime.getRuntime().availableProcessors();
我需要有关如何使此代码中的循环在加速因子达到特定值时停止执行的帮助或一些想法。这种方法的想法是不断 运行 不断增加的线程数量,并从结果中得出加速因子。四舍五入的加速因子是机器上有多少个内核。 运行 4 线程任务与 4 核机器上的 16 线程任务具有相同的加速因子。我希望不必手动将线程数设置为 运行。当加速因子稳定到一个值时,我希望程序终止。例如,如果加速因子已经稳定在 2,则无需 运行 测试 8、16 或 32 个线程。
4 核机器的示例输出:
测试的线程数:1 加速因子:1.0
测试的线程数:2 加速因子:1.8473736372646188
测试的线程数:4 加速因子:3.9416666666666669
测试的线程数:8 加速因子:3.9750993377483446
测试的线程数:16 加速因子:4.026086956521739
这台机器有:4 个内核
应用程序已完成执行。谢谢
private static void multiCoreTest() {
// A runnable for the threads
Counter task = new Counter(1500000000L);
// A variable to store the number of threads to run
int threadMultiplier = 1;
// A variable to hold the time it takes for a single thread to execute
double singleThreadTime = ThreadTest.runTime(1, task);
// Calculating speedup factor for a single thread task
double speedUp = (singleThreadTime * threadMultiplier) / (singleThreadTime);
// Printing the speed up factor of a single thread
System.out.println("Number of threads tested: " + threadMultiplier);
System.out.println("Speed up factor: " + speedUp);
// Testing multiple threads
while (threadMultiplier < 16) {
// Increasing the number of threads by a factor of two
threadMultiplier *= 2;
// A variable to hold the time it takes for multiple threads to
// execute
double multiThreadTime = ThreadTest.runTime(threadMultiplier, task);
// Calculating speedup factor for multiple thread tests
speedUp = (singleThreadTime * threadMultiplier) / (multiThreadTime);
// Message to the user
System.out.println("\n" + "Number of threads tested: "
+ threadMultiplier);
System.out.println("Speed up factor: " + speedUp);
}
// Print number of cores
System.out.println("\n" + "THIS MACHINE HAS: " + Math.round(speedUp)
+ " CORES");
System.out.println("\n"
+ "THE APPLICATION HAS COMPLETED EXECUTION. THANK YOU");
// Exiting the system
System.exit(0);
}
}
测试新加速是否与旧加速相同:
double oldSpeedUp = 0;
boolean found = false;
while(!found && threadMultiplier < 16) {
// ...
found = Math.round(speedUp) == Math.round(oldSpeedUp);
oldSpeedUp = speedUp;
}
附带说明一下,如果你想要核心数,你可以调用:
int cores = Runtime.getRuntime().availableProcessors();