一次可以在CPU上运行多少个线程 [英] How many threads can ran on a CPU at a time

查看:1067
本文介绍了一次可以在CPU上运行多少个线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我同样简单:

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.");
    }
}



它显示的输出如下:



处理器编号:2



主线程:3 Sun 5月26日19:23:19 IST 2013

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 2 Sun May 26 19:23:19 IST 2013

Child Thread Run:1 1 Sun 5月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 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 0 Sun May 26 19:23:19 IST 2013

Child Thread Run:1 4 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.

这怎么可能?

因为一个线程可以运行一次只有一个CPU,但它显示五个线程同时运行。

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

有没有办法显示只有一个线程可以在一个CPU中运行SAM e 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();

然后它显示输出如下:

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

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 5月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: 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: 2 99 Sun May 26 21:02:33 IST 2013

Child Thread Run:1 99 Sun May 26 21:02:34 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?

推荐答案

这取决于同时的含义。您可以通过切换在同一处理器上执行无限数量的线程,即从一个线程执行一行代码然后切换到另一个线程,执行一行代码,然后切换回来。处理器通过快速来回切换来模拟同时执行。

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.

然而,大多数处理器受限于 true simultaneous 线程的数量他们可以执行他们拥有的核心数量,但由于共享资源和硬件,即使这是一个糟糕的估计。理论上,在4核处理器上最多可以同时运行4个线程。

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.

这篇关于一次可以在CPU上运行多少个线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆