1

Does Java threads runs in parallel on Multi core Processor i.e, runs multiple threads at the same time?

[Parallel processing with Java Threads]

devv
  • 311
  • 5
  • 17
  • Also, even on a single CPU, a lot of threads are blocked on I/O wait, so it is useful to have multiple threads even without pre-emptive scheduling by the JVM (or operating system). – Thilo Jan 18 '16 at 07:54
  • Possible duplicate of [What does "volatile" mean in Java?](http://stackoverflow.com/questions/4885570/what-does-volatile-mean-in-java) – Erwin Bolwidt Jan 18 '16 at 08:06
  • 2
    Don't change the entire question in-flight. First you were asking about the volatile modifier, and now you have completely removed that section from your question, even though you already had answers to it. Your question was: "How does volatile is useful in single core processor and does the primary purpose of volatile is to flush each of cpu cache values to in Memory" – Erwin Bolwidt Jan 18 '16 at 08:52
  • Yes, I shouldn't have. Perhaps, i wanted to have main focus on parallelism – devv Jan 18 '16 at 08:57

3 Answers3

3

volatile is useful when you want to prevent your resource from being cached by Threads

Multiple threads can run on single CPU (though, one at a time) and can share resources, So volatile is still useful.

Adil Shaikh
  • 44,509
  • 17
  • 89
  • 111
2

JVM does not decide the number of processors to be used. It is the job of OS. JVM has the capability of creating multiple threads and submits them.

Volatile is used to guarantee the data is not being fetched from CPU cache during concurrency.

Thanga
  • 7,811
  • 3
  • 19
  • 38
0

First thing it's the JVM who spawns the threads but it's the hardware whom JVM depends on. If it has multi core, JVM can run multiple threads at the same time to extract max performance.

Now when it comes to user(You) decide to what extent you want to exploit the CPU resources and you do this through thread pools.(By defining max number of threads can run in parallel) but yet again you stuck up with your hardware configuration.

Helios
  • 851
  • 2
  • 7
  • 22