4
votes

I was just reading about "How does a server" handles multiple requests and ended up with java multithreading and multiple cores cpu.

During the days when the computers were single core and if there are 2 threads in java at the same time then jvm scheduler will take care of giving time to each thread. So, two threads never runs in parallel but makes use of ideal time (e.g. if a thread is blocked on some resource).

But is this is case with multiple cores and fork/join framework in java7 ? I mean i have heard that with multiple cores we can actually achieve parallelism. So, if i have two threads running, two cores cpu and each thread is given a separate core (it is not guaranteed though) then will they actually be parallel because jvm scheduler will still lead to time interleaving?

I am sorry if this question is stupid but i am really not sure on this one. Please help me to understand!!

Thanks! Ouney

1
Thread scheduling is actually delegated to the operating system. And yes, the OS will dispatch threads to all its cores, including Java threads.JB Nizet
What is your question? Are you asking whether a multi-threaded Java program can use more that one core on a multi-core platform? If so, the purely theoretical answer is "depends on the Java Virtual Machine, but for any practical JVM, the answer is "yes."Solomon Slow
@jameslarge- I am asking that do we achieve real parallelism in case of threads being assigned to multiple cores (one thread per core)? I got confused if it is true because jvm scheduler we still give time in turns (which indicates that two threads can never run in parallel)Ouney

1 Answers

1
votes

The problem is that it is still very hard to understand how many threads, cores, ... are actually available.

My personal suggestion: there are several articles on the java specialist newsletter which do a very deep dive into this subject.

For example this one: http://www.javaspecialists.eu/archive/Issue135.html

or a very new, on "the number of available processors": http://www.javaspecialists.eu/archive/Issue220.html