Reputation: 2017
I'm using a ThreadPoolExecutor, which is used for a batch process, but a single thread pool on a single JVM just uses 1 of the available server's CPUs. If fire a parallel second JVM with a new thread pool I get the the other CPU to be used.
So I'm wondering, a ThreadPoolExecutor uses a single underlying ThreadGroup for it's "workers", are these threads bound to a single CPU due to to the fact that they belong to a single ThreadGroup? If not, any others ideas?
See related question: Why I'm not using 100%?
Upvotes: 2
Views: 604
Reputation: 116878
a ThreadPoolExecutor uses a single underlying ThreadGroup for it's "workers", are these threads bound to a single CPU due to to the fact that they belong to a single ThreadGroup?
No. All threads in all JVMs are in the same ThreadGroup
unless you specifically change it. This in no way changes how the underlying native threads are scheduled so it is not the reason why you do not seem to be using multiple CPUs.
If not, any others ideas?
My first response is to question whether it actually is not using multiple CPUs. If you are looking at the CPU usage of the process, it may not be more than 100% because it is blocking on IO. This doesn't, however, mean that it is not using multiple CPUs. It may be that it is using many CPUs just that none of the tasks submitted to the thread-pool are "CPU bound". For example, if you run the following application then it should use multiple CPUs and should go above 100% CPU utilization (if you are on a system which measures CPU as a function of number of processors). If, however, you insert logging calls or reading from network or disk it may be limited by IO and not able to get above 100%.
public static void main(String[] args) {
ExecutorService threadPool = Executors.newCachedThreadPool();
for (int i = 0; i < 4; i++) {
threadPool.execute(new Runnable() {
public void run() {
long total = 0;
while (true) {
total++;
}
}
});
}
}
If fire a parallel second JVM with a new thread pool I get the the other CPU to be used.
It may be that on your architecture your JVM is not able to schedule on multiple CPUs. Certainly most of the common architectures and JVM variants are able to schedule to all available CPUs and multiple cores but maybe you are on an interesting configuration.
Upvotes: 1
Reputation: 11114
No they are not bound to a CPU. However, you can control CPU affinity if you want to experiment using the Java-Thread-Affinity library.
Upvotes: 1