I have a question about multithreading in Java with the multicores processor. As I know, when there was single core processor, multithreads worked as each thread would get a slice from cpu time to run, now we have multicores processor, as I assumed, JVM will be also updated to get this advantage, won't it ? Or it is not and that is the reason why fork/join framework was introduced in Java 7 for multi processing?
Thank you very much.
Asked
Active
Viewed 173 times
-2

Xitrum
- 7,765
- 26
- 90
- 126
-
4The JVM has supported multithreading since its first version. Fork/Join is a specialised way of breaking up a problem. I'm struggling to see what your actual question is? – tddmonkey Jan 18 '15 at 21:26
-
Which JVM? I'm sure most JVM implementations will take advantage of multiple cores when they execute multi threaded code. But it does depend on the implementation. Fork/Join framework is a higher level library that utilises java.lang.Thread. – Phil Jan 18 '15 at 21:36
-
I'm pretty sure the idea of which physical core a thread is running on is abstracted away by the operating system itself. So if your language supports threads at all then it'll support running threads on multiple cores, because it won't know – Richard Tingle Jan 18 '15 at 21:39
-
@MrWiggles Initially only "green threads" were supported. – Thorbjørn Ravn Andersen Jan 18 '15 at 21:43
-
If the question is really just, "can the JVM use multiple cores," this would be super easy to test. Just write a small program that defines a `Runnable` that does some work forever (`Random r = new Random(); while (true) { r.nextInt(); }` will do), and then spin up 20 threads that use that runnable. Run it, and look at your CPU utilization. – yshavit Jan 18 '15 at 21:45
-
(It's actually a _bit_ more complicated in theory; with a simple-enough test, the JVM could in theory realize that each thread is doing useless work and turn it into a no-op, if you never fetch the thread's results or print them out. So seeing no work wouldn't tell you anything, but seeing lots of work would be enough to prove that the JVM uses multiple cores. And that's what happens, at least on HotSpot 1.8.0_25 on Linux.) – yshavit Jan 18 '15 at 21:48
-
@Thorbjørn - I didn't state anything about green vs native, just that the JVM supported MT :) – tddmonkey Jan 18 '15 at 21:56
1 Answers
1
Yes, when there is a single core processor each thread will get a slice of the CPU time to run.
And yes, when there are multiple cores they will be distributed among them. But if there are more thread than cores (including threads from other processes/programs running in the computer) they will also only get a slice of the CPU.
If you have single thread, and you want to spawn more so that each can perform part of the work, then later you will have to join the results from them. Doing this forking (of data from one to many threads) and joining (of data from many threads to one) is more complicated then it seems at first, and to help with it is the reason why fork/join was introduced in Java 7.

Marcelo Glasberg
- 29,013
- 23
- 109
- 133