I have a quad core processor and the ThreadPoolExecutor is set to 4 core threads, but when I submit my callables (hundred or so) to the ThreadPoolExecutor, Java never uses more than 25% CPU. Why does it not use all of them?
Code in question:
static class Sum implements Callable{
private double bigarray[];
public Sum(double [] bigarray){
this.bigarray = bigarray;
}
@Override
public Double call(){
double sum = 0;
for (int i = 0; i < bigarray.length; i++){
sum += bigarray[i];
}
return sum;
}
}
In general, currently there is no interface in Java to control cores and processors affinity so your code (and threads) is scheduled by OS as it finds right. You might not like it.
As it was said well Running multiple threads on multiple CPU cores?
But I do not think this is a way to go.
See the answer to Stack Overflow question How does Java makes use of multiple cores?.