How many threads can ran on a CPU at a time

19,000

Solution 1

That depends on what you mean by "at the same time." You could have an infinite number of threads executed on the same processor via switching, i.e. executing one line of code from one thread and then switching to another, executing one line of code, and then switching back. The processor mimics "simultaneous execution" by switching back and forth really quickly.

However, most processors are limited on the number of true simultaneous threads they can execute to the number of cores they have, but even that is a bad estimate due to shared resources and hardware. In theory you could have up to 4 simultaneous threads running on a 4-core processor.

Solution 2

Every processor has some #number of cores and every core can run some #number of threads simultaneously. For ex: If a processor has 2 cores and each core can process 4 threads at a time simultaneously, then that processor can run 4*2=8 threads at any given instance of time.

Share:
19,000

Related videos on Youtube

Bill Woodger
Author by

Bill Woodger

Updated on September 29, 2022

Comments

  • Bill Woodger
    Bill Woodger over 1 year

    I want to know how many threads can be run on a CPU for a single application at the same time?

    I same a simple like:

    import java.awt.SystemColor;
    import java.util.Date;
    
    public class Threadcall {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            // TODO code application logic here
            System.out.println("--------------------------");
            System.out.println(Runtime.getRuntime().availableProcessors());
            System.out.println("--------------------------");
            for (int i = 0; i < 5; i++) {
                new samplethread(i);
            }
            // create a new thread
            //samplethread1.run();
            try {
                for (int i = 5; i > 0; i--) {
                    System.out.println("Main Thread: " + i + "\t" + new Date());
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                System.out.println("Main thread interrupted.");
            }
            System.out.println("Main thread exiting.");
        }
    }
    
    public class samplethread implements Runnable {
    
        Thread t;
    
        samplethread(int i) {
            // Create a new, second thread
            t = new Thread(this, Integer.toString(i));
            System.out.println("Child thread Creation NO: " + i + "\t" + t.getName());
    
    
    
            t.start(); // Start the thread
            // t.run();
    
        }
    
        @Override
        public void run() {
    
            try {
                for (int i = 5; i > 0; i--) {
    
                    System.out.println("Child Thread Run: " + i + "\t" + t.getName() + "\t" + new Date());
                    // Let the thread sleep for a while.
                    System.out.println("****************************");
                    Thread.sleep(500);
                }
            } catch (InterruptedException e) {
                System.out.println("Child interrupted.");
            }
            System.out.println("Exiting child thread.");
        }
    }
    

    It shows the output like:

    Processor Number: 2

    Main Thread: 3 Sun May 26 19:23:19 IST 2013

    Child Thread Run: 1 2 Sun May 26 19:23:19 IST 2013

    Child Thread Run: 1 1 Sun May 26 19:23:19 IST 2013

    Child Thread Run: 1 3 Sun May 26 19:23:19 IST 2013

    Child Thread Run: 1 0 Sun May 26 19:23:19 IST 2013

    Child Thread Run: 1 4 Sun May 26 19:23:19 IST 2013

    From the output we can see that at the same moment five threads can execute (I even do print the result in milliseconds)..........I have two processor which is reported in the programme.

    How is this possible?

    Because one thread can run in only one CPU at a time but it shows that five threads run at the same time.

    Is there any way to show that only one thread can ran in one CPU at the same time.......

    If I modify my code like:

    t.start();
    t.join();
    

    Then it shows the output like:

    Child thread Creation NO: 99 99 Child Thread Run: 5 99 Sun May 26 21:02:32 IST 2013

    Child Thread Run: 4 99 Sun May 26 21:02:32 IST 2013

    Child Thread Run: 3 99 Sun May 26 21:02:33 IST 2013

    Child Thread Run: 2 99 Sun May 26 21:02:33 IST 2013

    Child Thread Run: 1 99 Sun May 26 21:02:34 IST 2013

    So how is it possible that if I add a simple line in the code then it shows that only two threads can access two processors?

    • Sergey Zyuzin
      Sergey Zyuzin almost 11 years
      You show time till seconds. Thread switching happens much faster - so you don't noticе it here. When you do 'sleep' thread doesn't occupy CPU and it is free to execute other stuff.
    • Christian Fries
      Christian Fries over 4 years
      Your time resolution is too small. You should print the result in nano seconds.
  • Sergey Zyuzin
    Sergey Zyuzin almost 11 years
    Each thread also consumes memory for its stack. On windows it is 1 MB by default. So number of threads is also limited by available memory.
  • Jason
    Jason almost 11 years
    true, infinite limited to the resources of the machine and os
  • Admin
    Admin almost 11 years
    @jason if I modify my code like t.start(); t.join(); then It shows the output like: Child thread Creation NO: 99 99 Child Thread Run: 5 99 Sun May 26 21:02:32 IST 2013 Child Thread Run: 4 99 Sun May 26 21:02:32 IST 2013 Child Thread Run: 3 99 Sun May 26 21:02:33 IST 2013 Child Thread Run: 2 99 Sun May 26 21:02:33 IST 2013 Child Thread Run: 1 99 Sun May 26 21:02:34 IST 2013 so what how it possible that if I add a simple line in the code then it shows that only 2 thread can access 2 processor?