java 一个 CPU 上一次可以运行多少个线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16760689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 23:55:35  来源:igfitidea点击:

How many threads can ran on a CPU at a time

javamultithreading

提问by Jason

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

我想知道单个应用程序可以在一个 CPU 上同时运行多少个线程?

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

处理器数量:2

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

主线程:2013 年 5 月 26 日星期日 19:23:19 IST

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

子线程运行: 1 2 Sun May 26 19:23:19 IST 2013

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

子线程运行:1 1 星期日 5 月 26 日 19:23:19 IST 2013

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

子线程运行:1 3 5 月 26 日星期日 19:23:19 IST 2013

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

子线程运行:1 0 星期日 5 月 26 日 19:23:19 IST 2013

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

子线程运行: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.

因为一个线程一次只能在一个 CPU 上运行,但它显示五个线程同时运行。

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

有什么办法可以说明一个CPU只能同时运行一个线程......

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

子线程创建编号:99 99 子线程运行:5 99 Sun May 26 21:02:32 IST 2013

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

子线程运行:4 99 Sun May 26 21:02:32 IST 2013

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

子线程运行:3 99 Sun May 26 21:02:33 IST 2013

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

子线程运行:2 99 星期日 5 月 26 日 21:02:33 IST 2013

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

子线程运行: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?

那么,如果我在代码中添加一个简单的行,它怎么可能显示只有两个线程可以访问两个处理器?

回答by Jason

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

然而,大多数处理器的真正并发线程数量受限于它们拥有的内核数量,但由于共享资源和硬件,即使这是一个错误的估计。理论上,一个 4 核处理器上最多可以同时运行 4 个线程。

回答by rohegde7

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=8threads at any given instance of time.

每个处理器都有一些#number 的内核,每个内核可以同时运行一些#number 的线程。例如:如果一个处理器有 2 个内核,并且每个内核可以同时处理 4 个线程,那么该处理器可以4*2=8在任何给定的时间实例中运行线程。