问题
I've asked the question on unix.stackexchange as well...
Can the openjdk 8 (on ubuntu) run more than 4 threads
(i.e.: use the 8 cores available) on a system?
Edit: I have 12 Thread objects which run simultaneously (they all produce output) but the linux top program indicates there are only 4 cores occupied with the java process (when scaling from 1 Thread to 2, 3 and 4 I didn't have this problem)...
Edit2: top output:
%Cpu(s): 41.8 us
6982 ubuntu 20 0 8766844 1.7g 18684 S 352.0 11.6 26:07.56 java
lscpu output:
CPU(s): 8
On-line CPU(s) list: 0-7
Thread(s) per core: 2
Core(s) per socket: 4
Socket(s): 1
Edit3:
Agent[] agentz = new Agent[4]; //implements Runnable
agentz[0] = new Agent(botMovez[0], playerIndex);
for (int i = 1; i < 4; i++) {
agentz[i] = new Agent(botMovez[i], playerIndex, boardCopies[i - 1]);
}
Thread[] threadz = new Thread[4];
for (int i = 0; i < 4; i++) {
threadz[i] = new Thread(agentz[i]);
threadz[i].start();
}
for (int i = 0; i < 4; i++) {
try {
threadz[i].join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
-> this code gets executed in a Runnable which is started like:
(new Thread(new Calculator(this, performedMoves))).start();
Edit4:
public class Test {
public static void main(String[] args) {
Test test = new Test();
TestAgent[] testAgentz = new TestAgent[8];
Thread[] threadz = new Thread[8];
for (int i = 0; i < 8; i++) {
testAgentz[i] = test.new TestAgent();
threadz[i] = new Thread(testAgentz[i]);
threadz[i].start();
}
System.out.println("waiting to join...");
for (int i = 0; i < 8; i++) {
try {
threadz[i].join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println("unreachable code");
}
private class TestAgent implements Runnable {
@Override
public void run() {
while (true) {
}
}
}
}
-> This code does occupy 800% cpu
So it must be either Spring boot or Tomcat...
I'm going to ask another question because this one is getting to cluttered...
回答1:
I see the output of lscpu
give 2 threads per core. You don't have an 8 core machine. Try running something like cat /dev/urandom > /dev/null
8 times. Probably you sill don't go over 400%.
来源:https://stackoverflow.com/questions/65844362/can-the-openjdk-8-jvm-use-more-than-4-cores