Meaningless Drivel is fun!*
The moose likes Threads and Synchronization and the fly likes Threads running on single-core or multi-core? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads running on single-core or multi-core?" Watch "Threads running on single-core or multi-core?" New topic
Author

Threads running on single-core or multi-core?

Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

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?


SCJP, SCJD, SCWCD, SCBCD, SCEA
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Of course multiple. To prove it just run this and check out your cpu load



"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Jiafan Zhou
Ranch Hand

Joined: Sep 28, 2005
Posts: 192

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

Joined: Jul 08, 2003
Posts: 24183
    
  34

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

Joined: Mar 22, 2000
Posts: 12769
    
    5
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
 
 
subject: Threads running on single-core or multi-core?