Multi-CPU Thread Execution
I'm really curious on how threads execute when the JVM is given multiple processors. So, I designed a very simple application that starts a second thread off of the main at the same time and executes. Both threads do the same thing and sleep at the same time. The overhead question for me here was, "does the JVM recognize and work with multiple processors on thread execution? Will it use multiple cores to execute multiple threads simultaneously?"
The results were very interesting and not exactly what I expected. Rather than try to explain them to you or post screenshots, I'll give you the source code for you to copy, and run it under TWO scenarios.
First, (and this only applies to those running a machine with more than one core) just run the program and observe the results of how the threads execute.
Second, run the program, but do not hit a key yet at the scanner line to start it. Open Windows Task Manager (or the equivalent on your platform) and change the processor affinity so that the Java machine process has only ONE processor to work with. Observe those results.
If any of you would care to explain to me how Java works with multiple cores, and if it works concurrently, please do so. Thanks!
Code :
import java.util.Scanner;
class ThreadTest {
public void startThreads() {
Scanner sc = new Scanner(System.in);
sc.nextLine();
Thread t = new Thread(new SecondThread());
t.start();
for(int i=50;i>0;i--) {
System.out.println("Main thread working");
try { Thread.sleep(1000); } catch(InterruptedException e) {}
}
System.out.println("Main is done");
}
class SecondThread implements Runnable {
public void run() {
for(int i=50;i>0;i--) {
System.out.println("Second thread is working");
try { Thread.sleep(1000); } catch(InterruptedException e) {}
}
System.out.println("Second thread is done");
}
}
public static void main(String[] args) {
ThreadTest tt = new ThreadTest();
tt.startThreads();
}
}
Re: Multi-CPU Thread Execution
Quote:
"does the JVM recognize and work with multiple processors on thread execution? Will it use multiple cores to execute multiple threads simultaneously?"
It depends on which JVM you use. The "official" one provided by Oracle uses native threads, therefore all handling of thread scheduling is done by the OS.
If you OS supports the use of multiple cores (all modern mainstream OS's do), then the JVM can take advantage of the multiple cores on your computer.
It's very difficult to predict how the OS will schedule the different threads as all threads running on the computer (not just those being used by your Java program) must share CPU time.
Re: Multi-CPU Thread Execution
Also if you are keenly interested then this may be of some help to you.
Re: Multi-CPU Thread Execution
Quote:
Originally Posted by
helloworld922
It depends on which JVM you use. The "official" one provided by Oracle uses native threads, therefore all handling of thread scheduling is done by the OS.
If you OS supports the use of multiple cores (all modern mainstream OS's do), then the JVM can take advantage of the multiple cores on your computer.
It's very difficult to predict how the OS will schedule the different threads as all threads running on the computer (not just those being used by your Java program) must share CPU time.
Interesting. How does using the Java 5 java.util.concurrent threads change things?
Re: Multi-CPU Thread Execution
Quote:
Originally Posted by
bgroenks96
Interesting. How does using the Java 5 java.util.concurrent threads change things?
That package was introduced in order to achieve high-level concurrency using the thread objects.
1. Creating and managing thread pools.
2. Concurrent collections such as Queue,BlockedQueue
3. Atomic variables, Synchronizers, Locks, Nano-second timing etc
Overall, a wonderful package to develop and test Real-time applications over Java with improved reliability and performance.