permaculture playing cards*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Multiple Processors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Multiple Processors" Watch "Multiple Processors" New topic
Author

Multiple Processors

Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
In a computer with 2 processors, when I create 4 threads, 2 will be on Running state and 2 on Ready?
Thanks in advance,
Wagner Danda
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Wagner Danda:
In a computer with 2 processors, when I create 4 threads, 2 will be on Running state and 2 on Ready?
Thanks in advance,
Wagner Danda

I don't think so. This really comes down to the implementation of your operating system. The JVM (which your application runs on) is really just one of many threads being executed by your operating system. For example, if you're running Windows, a number of threads are bring run at any one time. Which ones are running and which ones are ready is entirely up to the operating system. Since the JVM is just one thread that's being implemented, it will only run on one processor at a time (and even then for it's timeslice). That process will then switch back and forth between your four threads, so they'll probably never be executing on both processors at once.
I guess this really boils down to how your operating system handles multiple processes, but I'd develop your code as if there was only one processor inside your box. Forget that there are two of them because only the operating system itself can really take advantage of that.
This area isn't really my forte, so I'm kinda guessing here. Perhaps someone else can give a more enlightened answer, but this is what I think, anyway.
Corey


SCJP Tipline, etc.
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Techniclly, the JVM is a process running on the native OS. It's an important distinction, because a process is scheduled for execution by that native OS, whereas a java Thread is scheduled by the JVM.
This means that if you are running several VMs on one machine, it's the native OS that determines how each of those VM processes get processor time or assigned to multiple processors.
The VM's own internal Thread-scheduler determines how much time and when a Thread runs; however, I believe the VM thread scheduler eventually has to call native OS code to give the thread some processor time, so at some point there is the ability to schedule multiple threads on different CPUs. But I think it's more a question of how the particular JVM on your platform handles thread scheduling than the native OS.
Rob


Rob
SCJP 1.4
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

It's possible, but will not happen automatically, at least not on all platforms. Processes, viewed as kernel state structures, are built on the notion of a single address space; that model assumes, even at a very, very small level, things are happening one at a time. Imagine what could happen then if two threads in the same process tried to acquire the same object lock at the same moment in time. You could then evade the OS's normal defense for ensuring that locks work in the first place. The skies would turn to blood, the seventh seal would break, people would run with scissors and drink milk from the carton -- it would be awful.
Any responsible OS applies a fencing scheme to processes to safeguard against unintended behavior like this. The process must then request the ability to runs its threads on multiple CPUs. In doing so, the code 'acknowledges' that it has written-in protection against parallel execution.


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Corey, that's not completely true:
Here is an excerpt of Java Threads by Scott Oaks and Henry Wong (O'Reilly)

...
When our Java program runs on a machine with multiple processors, the following assumptions become very important:
- We can no longer assume that a currently running thread has the highest priority. A higher-priority thread may be running on a different processor.
- We can no longer assume that a low-priority thread will not run. There may be enough processors to give it execution time.
- We can no longer assume that threads of different priorities will not be running at the same time.
- We can no longer assume that certain race conditions can be ignored because it is "unreasonable" for a particular case to occur....

This is to illustrate that Java threads may be executing outside the JVM process... Again, it depends on the implementation, but it is possible to parallelize Java programs...
HIH
[ January 21, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Wagner Danda
Greenhorn

Joined: Jul 10, 2001
Posts: 26
So, if I am in a timeslice OS (with 2 processors), and I write this:

Will these 4 threads compete each other to one CPU (with JVM)? Or will they fight for the 2 CPU's? Is this up to the JVM implementation?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Originally posted by Valentin Crettaz:
Corey, that's not completely true...

A ha! So my assumption was entirely true! This area is, indeed, not my forte!
Thanks for the correction.
Corey
[ January 21, 2002: Message edited by: Corey McGlone ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Multiple Processors