If cores is less than one, either your processor is about to die, or your JVM has a serious bug in it, or the universe is about to blow up. If you want to get number of physical cores you can run cmd and terminal command and then to parse the output to get info you need. Below is shown function that returns number of physical cores. This is an additional way to find out the number of CPU cores and a lot of other information , but this code requires an additional dependence:. If you want to dubbel check the amount of cores you have on your machine to the number your java program is giving you.
Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more.
Asked 10 years, 9 months ago. Active 1 year ago. Viewed k times. How can I find the number of cores available to my application from within Java code? Improve this question. Damir Damir 50k 90 90 gold badges silver badges bronze badges. Finding the number of cores the Java program can use at startup is easy, using Runtime. Logical or physical cores? There's an important difference. Also see: stackoverflow.
Add a comment. Is there a way in Java to get this info? Improve this question. Roman Roman Great question! Thanks for asking it — Dream Lane. Add a comment. Active Oldest Votes. You can use Runtime. Improve this answer. John Vint John Vint But it does tell it how many are available to the JVM Stephen C: yes, I need exactly that. But, could you enlighten me a little: when is it possible that not all of the processors are available to the JVM? Cajunluke Cajunluke 3, 27 27 silver badges 28 28 bronze badges.
Stephen Gennard Stephen Gennard 1, 16 16 silver badges 21 21 bronze badges. I believe this will provide the actual number of processors installed, not the number available. That's a rather difficult and non-portable way of doing this. The good news is that if you just start threads, they will be scheduled to multiple cores fine. The following code produces the following output on a 4-core server, you can see that up to 4 cores the total performance is approximately linear as the code is executed in parallel.
However, when a thread pool starts up, with the intent to start one thread per CPU core, it typically uses the Runtime. It starts that many threads, to get the work distributed over all the cores. In my case, the Runtime. Therefore only one thread was started, and all my code ran sequentially, even though multiple cores were available and would have been used if the threads had been started.
Imagine if you bought a dual-core laptop. You started Word. You started Excel. Then you tried to start a browser and Windows told you "you can't start any more tasks. Word might use the whole of a single core. Excel might use the whole of the other single core. You only have two cores.
0コメント