我正在处理并发分配,其中涉及并行化性能问题。该问题涉及大量的阻塞 I/O,因此在我的报告中,我想使用并比较各种方法的 CPU 使用情况。
我是分析新手,我已经开始使用 Java 的虚拟虚拟机,但即使多个线程运行无阻塞的紧密循环,我似乎也无法达到 50% 以上的 cpu 使用率。这似乎是我的两个核心中只使用了一个。
如何让我的线程使用两个核心?我尝试过手动创建线程和使用执行器框架。
提前致谢
最佳答案
我不知道你的代码在做什么,但这设法让我的所有核心达到 100%...
import java.util.concurrent.*;
public class Test implements Runnable {
public static void main(String[] args) throws InterruptedException {
Test task = new Test();
int threads = Runtime.getRuntime().availableProcessors();
ExecutorService pool = Executors.newFixedThreadPool(threads);
for (int i = 0; i < threads; i++) {
pool.submit(task);
}
pool.awaitTermination(120, TimeUnit.SECONDS);
}
@Override public void run() {
System.out.println("Task running...");
int i = 0;
while (true) {
i++;
}
}
}
关于java - 无法让java使用多个核心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7685747/