Thanks in advance,
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,
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.
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.
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.
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...
[ January 21, 2002: Message edited by: Valentin Crettaz ]
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?
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.
[ January 21, 2002: Message edited by: Corey McGlone ]