Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads running on single-core or multi-core?

 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If creating multiple threads on a multi-core machine, does JVM run multiple threads on a single core or multiple cores? And how to prove that?
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course multiple. To prove it just run this and check out your cpu load

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Of course multiple.


In theory, it actually depends on the JVM implementation. Many early JVMs were based on the Green Threads library, which implemented threads within a single OS process. If run on a multiprocessor machine, such a JVM would still run on a single processor; the same would hold for today's multicore single processors.

Today, all major OSs support threading at the OS level, and all major JVMs use the platform threads library more or less directly. So it's up to the OS to schedule threads on multiple cores. For Windows, this wasn't happening until Windows XP SP2, with a few additional patches, so you can't assume it will be true on all machines.
 
Jiafan Zhou
Ranch Hand
Posts: 193
Fedora Linux Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wouter Oet wrote:Of course multiple. To prove it just run this and check out your cpu load



I need a little bit more to prove it rather than checking the CPU load.

I wish that there is a Java API to tell me the CPU core number that a particular thread is running on. For example, thread1, it is running on CPU core1. Thread 2, it is running on CPU core 2.

Or even check it a similar way on an Operation System level. I am using Fedora as the OS.

Is it possible?

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't generally work that way; a thread will get scheduled on more than one core during its lifetime. It can move from core to core, processor to processor.

The psutils give you the opportunity to look into threads on Linux; try "ps -m" as a start.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Windows NT (1993) used multiple CPUs way before XP SP2. I was able to get 60-70% utilization of both on a 2 cpu machine after painstaking separation of the processes with ordinary Java.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic