Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Multiple Processors

 
Wagner Danda
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Ross
Bartender
Posts: 2205
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Wagner Danda
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic