我用 Java 编写了一个(非常简单的)基准测试。它只是将 double 值增加到指定值并花费时间。
当我在我的 6 核台式机上使用此单线程或少量线程(最多 100 个)时,基准测试返回合理且可重复的结果。
但是当我使用例如 1200 个线程时,平均多核持续时间明显低于单核持续时间(大约 10 倍或更多)。我已确保增量的总量是相同的,无论我使用多少线程。
为什么线程越多性能越差?有解决这个问题的技巧吗?
我正在发布我的源代码,但我认为这没有问题。
基准测试.java:
package sibbo.benchmark;
import java.text.DecimalFormat;
import java.util.LinkedList;
import java.util.List;
public class Benchmark implements TestFinishedListener {
private static final double TARGET = 1e10;
private static final int THREAD_MULTIPLICATOR = 2;
public static void main(String[] args) throws InterruptedException {
Benchmark b = new Benchmark(TARGET);
b.start();
}
private int coreCount;
private List<Worker> workers = new LinkedList<>();
private List<Worker> finishedWorkers = new LinkedList<>();
private double target;
public Benchmark(double target) {
this.target = target;
getSystemInfos();
printInfos();
}
private void getSystemInfos() {
coreCount = Runtime.getRuntime().availableProcessors();
}
private void printInfos() {
System.out.println("Usable cores: " + coreCount);
System.out.println("Multicore threads: " + coreCount * THREAD_MULTIPLICATOR);
System.out.println("Loops per core: " + new DecimalFormat("###,###,###,###,##0").format(TARGET));
System.out.println();
}
public synchronized void start() throws InterruptedException {
Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
System.out.print("Initializing singlecore benchmark... ");
Worker w = new Worker(this, 0);
workers.add(w);
Thread.sleep(1000);
System.out.println("finished");
System.out.print("Running singlecore benchmark... ");
w.runBenchmark(target);
wait();
System.out.println("finished");
printResult();
System.out.println();
// Multicore
System.out.print("Initializing multicore benchmark... ");
finishedWorkers.clear();
for (int i = 0; i < coreCount * THREAD_MULTIPLICATOR; i++) {
workers.add(new Worker(this, i));
}
Thread.sleep(1000);
System.out.println("finished");
System.out.print("Running multicore benchmark... ");
for (Worker worker : workers) {
worker.runBenchmark(target / THREAD_MULTIPLICATOR);
}
wait();
System.out.println("finished");
printResult();
Thread.currentThread().setPriority(Thread.NORM_PRIORITY);
}
private void printResult() {
DecimalFormat df = new DecimalFormat("###,###,###,##0.000");
long min = -1, av = 0, max = -1;
int threadCount = 0;
boolean once = true;
System.out.println("Result:");
for (Worker w : finishedWorkers) {
if (once) {
once = false;
min = w.getTime();
max = w.getTime();
}
if (w.getTime() > max) {
max = w.getTime();
}
if (w.getTime() < min) {
min = w.getTime();
}
threadCount++;
av += w.getTime();
if (finishedWorkers.size() <= 6) {
System.out.println("Worker " + w.getId() + ": " + df.format(w.getTime() / 1e9) + "s");
}
}
System.out.println("Min: " + df.format(min / 1e9) + "s, Max: " + df.format(max / 1e9) + "s, Av per Thread: "
+ df.format((double) av / threadCount / 1e9) + "s");
}
@Override
public synchronized void testFinished(Worker w) {
workers.remove(w);
finishedWorkers.add(w);
if (workers.isEmpty()) {
notify();
}
}
}
worker .java:
package sibbo.benchmark;
public class Worker implements Runnable {
private double value = 0;
private long time;
private double target;
private TestFinishedListener l;
private final int id;
public Worker(TestFinishedListener l, int id) {
this.l = l;
this.id = id;
new Thread(this).start();
}
public int getId() {
return id;
}
public synchronized void runBenchmark(double target) {
this.target = target;
notify();
}
public long getTime() {
return time;
}
@Override
public void run() {
synWait();
value = 0;
long startTime = System.nanoTime();
while (value < target) {
value++;
}
long endTime = System.nanoTime();
time = endTime - startTime;
l.testFinished(this);
}
private synchronized void synWait() {
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
最佳答案
您需要了解操作系统(或 Java 线程调度程序,或两者)正在尝试在应用程序中的所有线程之间取得平衡,让它们都有机会执行某些工作,并且成本非零在线程之间切换。使用 1200 个线程,您刚刚达到(并且可能远远超过)临界点,在该临界点中,处理器花费的上下文切换时间多于实际工作时间。
这是一个粗略的类比:
你在 A 房间里有一份工作要做。你每天在 A 房间里站 8 个小时,然后完成你的工作。
然后你的老板过来告诉你,你也必须在 B 房间做一份工作。现在你需要定期离开房间 A,沿着大厅走到 B 房间,然后再走回去。每天步行需要 1 分钟。现在,您在每项工作上花费 3 小时 59.5 分钟,并在房间之间步行一分钟。
现在假设您有 1200 个房间要工作。与实际工作相比,您在房间之间行走的时间会更多。这就是您将处理器置于的情况。它花费了太多时间在上下文之间切换,以至于没有完成任何实际工作。
编辑:现在,根据下面的评论,也许你在继续之前在每个房间里花费了固定的时间——你的工作会取得进展,但房间之间的上下文切换次数仍然会影响单个任务的整体运行时间.
关于java - 使用太多线程的问题基准程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11346899/