2

In a java program, thread scheduler switches between threads rapidly so we get the feeling of many tasks happening at the same time when its actually not the case.

However if our computers have multiple central processing units (CPUs), would that mean the threads actually processed parallel without the above mentioned switching?

Justification against the possible duplicate suggestion: I am asking whether the java program is able to utilize the multiple cores as opposed to that question's "how does JVM make use of multiple cores?"

GhostCat
  • 137,827
  • 25
  • 176
  • 248
Ran_Macavity
  • 154
  • 2
  • 21
  • 2
    Yes. Threads can be executed concurrently by running on different cores. – JB Nizet Sep 08 '17 at 18:43
  • 1
    When the underlying operating system is exploiting hardware based threads then the JVM and any Java applications inside the JVM can run in parallel. Your assumption that this is even doubtful... reads like your knowledge is like 10 years outdated. – GhostCat Sep 08 '17 at 18:55
  • Sorry for posting this but could not help but posting. True this might be a duplicate of another question but many good questions gets downvoted based on numerous reasons.Most of people are experts in their respective technology area here but what everyone must understand is not everyone of us are like that. Being newbie to a technology ( in my case java) we might ask some opinion based questions and it IMO it should not be discouraged by downvoting.I understand what is a bad question and do not have anything against downvoting such a question.But I humbly request tolerate us newbies as well. – Ran_Macavity Sep 08 '17 at 18:59
  • Votes are feedback. You see - most people don't understand that they should do serious research prior posting a question. I don't see much evidence of prior research here. – GhostCat Sep 08 '17 at 19:13
  • @GhostCat agree on your point but I did some research before posting here. The few places I have visited,there has been some contradicting facts whether a program can utilize multiple cores or not.So posted to get a conclusive answer.Its my fault not to search more thoroughly in this site.Thanks for the reply and answer :) – Ran_Macavity Sep 08 '17 at 19:20
  • 1
    And just for the record : most modern CPUs support multiple threads in hardware. So even a single CPU can give you true parallelism. – GhostCat Sep 09 '17 at 04:31
  • I don't understand the distinction you're trying to make. What is the difference between a program using multiple cores vs. the VM using multiple cores? – Kevin Krumwiede Sep 09 '17 at 04:33
  • @KevinKrumwiede I am still learning java so I asked this.I really do not get what you are asking? Isn't it the same at a java program runtime? (program and VM) – Ran_Macavity Sep 10 '17 at 13:44
  • You asked how a Java program uses multiple cores as opposed to how the VM uses multiple cores. I don't understand the distinction. – Kevin Krumwiede Sep 10 '17 at 21:34
  • @KevinKrumwiede The last paragraph was a justification made by me since this was marked as a duplicate question.My original question did not was the content above that paragraph. – Ran_Macavity Sep 11 '17 at 17:44

1 Answers1

1

Yes, using multiple threads in Java on a machine with multiple physical processors will result in actual parallel processing, unless you specifically tell the O/S to force all of your application's threads to run on the same physical processor.

CodeBlind
  • 4,519
  • 1
  • 24
  • 36