multi Cpu and threads

Hi all,

I work on a dual Pentium CPU with Win NT 4 and use Java3D. When I monitor the CPU load when running a java 3d application then both CPUs are working for that application.

Now I started to learn about threads and noticed that there is a method called currentThread() returning a single referece. So my question is: if there is only a single thread running at a time, how can it be spread over several CPUs or what thread is referenced with this reference if there are more than one thread really in parallel ?

Can somebody please explain how the VM handles threads on a multi CPU machine ? (in the tutorials this is not explained).

Thanks

Paul

[694 byte] By [pszawlow] at [2007-9-26 8:04:12]
# 1

>method called currentThread() returning a single

>referece...if there is only a single thread running at a time,

When you call currentThread() you are in a thread, otherwise your code wouldn't be running. That method returns that thread. That is all it is supposed to do.

jschell at 2007-7-1 18:24:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 2

Since you are using Java3d you should know that your program is multi-threaded. Java 3d uses multiple threads for rendering and to use the AWT event system. That is why you are seeing both processors at max utilitization.It also uses multiple threads to do things like run finalization and garbage collection.

As far as HOW java uses multiple threads and spreads them over multiple processors is a little bit fuzzier. If you really need to know, then read the Java Virtual Machine Spec. It's in products and APIs. A single thread cannot be spread over multiple cpus at the same time. However, it is not guarenteed to be run in the same CPU every time though.Your OS ( not the JVM) has control of where a thread is physically going to be run. You can change this setting usually, in windows it's a registry setting and in linux I think it might be a compile option ( not sure on that one). So that if you want, you can force a program to always be run in the same cpu.

steveftoth at 2007-7-1 18:24:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 3

>if there is only a single thread running at a time

Who says this is the case? In multi-CPU systems there could be more than one thread running at a time.

>what thread is referenced with this reference

The thread returned by the currentThread() method is the one that is running the code that calls the currentThread() method. That thread is guaranteed to be running, of course, but other threads could also be running. Perhaps you are misinterpreting "currentThread()" to mean "the thread that is active at this instant in the JVM"?

>Can somebody please explain how the VM handles threads on a multi CPU machine ? (in the tutorials this is not explained).

I'm not sure what explanation is necessary here. The VM has a bunch of threads that it needs to run. If there is only one CPU, it will run them all "simultaneously" by rapidly switching between them all, subject to locks and priorities and the like. If there is more than one CPU available to it, it will do exactly the same thing, except that now it has to decide which CPU will handle which thread. But that's an implementation detail of the JVM that should not affect the application programmer's design; that's probably why the tutorials don't go there.

DrClap at 2007-7-1 18:24:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 4
>...priorities and the like ...that should not affect the application.Presuming of course that the design takes into account the possibility that a lower priority thread might not run at all, right?
jschell at 2007-7-1 18:24:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 5

> The thread returned by the currentThread() method is

> the one that is running the code that calls the

> currentThread() method. That thread is guaranteed to

> be running, of course, but other threads could also be

> running. Perhaps you are misinterpreting

> "currentThread()" to mean "the thread that is active

> at this instant in the JVM"?

Yep, I was misinterpreting currentThread in the way you supected. It should be interpreted as current thread of the CPU which executes the code which calls currentThread.

Thanks to all for answering.

pszawlow at 2007-7-1 18:24:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 6
Is it true for all OSes and JVM versions that threads will be scheduled across mulitple processors? Or is there a best combination, like Linux with JDK1.2.2+.
Tony Pladies at 2007-7-1 18:24:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...
# 7

>...that threads will be scheduled across mulitple processors?

Not according to "Jave Threads 2nd edition". It claims that on a sun box that only a single OS thread exists (regardless of what your app does) until it blocks on a system resource. Then the OS spins another thread. So you might end up using only a single CPU. It provides a JNI method to assure that a minimum number of threads are spun up.

jschell at 2007-7-1 18:24:59 > top of Java-index,Java HotSpot Virtual Machine,Specifications...