What is meant by a hardware thread. Is it always double the number of processor co开发者_运维知识库res available? How to determine the number of hardware threads in an Intel Core2 Duo processor? Can it be determined through Java code?
What is meant by a hardware thread.
You tell us. It's not really an established term.
Is it always double the number of processor cores available?
Ah, now it seems like you're talking about hyperthreading where partial redundancies within a CPU core are used to "fake" an additional core.
How to determine the number of hardware threads in an Intel Core2 Duo processor? Can it be determined through Java code?
You could try to find something in the system properties, but it's not going to be standard (i.e. depends on the JVM and OS).
Personally, I'd just use a sensible default (4 or 8 threads - having a few too many is perferable to not using available cores) and make it configurable.
It's definately not the case that the number of hardware threads is twice the number of cores -- AMD processors don't have any sort of hyperthreading ( yet ), and hyperthreading may be turned off in the BIOS (as is usually was for pre-nehalem processors, as it really didn't buy you very much in practice). There's also no reason why it would necessarily have to only be two hardware threads per core -- Power7 systems have 4 per core.
There's no OS- or hardware-independant way to do these checks. The only library I know that fairly portably can query this information is the hardware locality (hwloc) library . It's a C library, so should be pretty simple to call from Java.
I suppose you meant the number of CPUs. You can get it by:
int N_CPUS = Runtime.getRuntime().availableProcessors();
Using this value you can determine how many threads should exists in your thread pool if you plan to implement it. To read more about thread pool size you can in Java in Concurrency, Section 8.2.
精彩评论