Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Multi-CPU Thread Execution

  1. #1
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default 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!

    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();
      }
    }


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Multi-CPU Thread Execution

    "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.

  3. #3
    Junior Member Mrc0d3r's Avatar
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Multi-CPU Thread Execution

    Also if you are keenly interested then this may be of some help to you.

  4. #4
    Member
    Join Date
    Jun 2011
    Posts
    182
    My Mood
    Where
    Thanks
    15
    Thanked 8 Times in 8 Posts

    Default Re: Multi-CPU Thread Execution

    Quote Originally Posted by helloworld922 View Post
    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?

  5. #5
    Junior Member Mrc0d3r's Avatar
    Join Date
    Jun 2011
    Location
    TCP/IP Layer 3
    Posts
    25
    My Mood
    Bored
    Thanks
    0
    Thanked 6 Times in 5 Posts

    Default Re: Multi-CPU Thread Execution

    Quote Originally Posted by bgroenks96 View Post
    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.

Similar Threads

  1. Having Difficulties With a Multi Thread Code... Help?
    By Allicat in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 5th, 2011, 01:35 AM
  2. Converting Code to Multi Thread
    By cmill_xc in forum Threads
    Replies: 1
    Last Post: December 6th, 2010, 12:39 PM
  3. Question on dependency execution
    By back2grave in forum Algorithms & Recursion
    Replies: 2
    Last Post: May 2nd, 2010, 01:50 AM
  4. Java GUi program Execution
    By Rajan in forum AWT / Java Swing
    Replies: 2
    Last Post: April 19th, 2010, 09:48 PM
  5. Not Looping? (do - while) bad execution!
    By chronoz13 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 23rd, 2009, 08:51 PM