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

标签 java multithreading

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

我喜欢一个简单的:

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

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

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

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

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

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

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

从输出中我们可以看到在同一时刻可以执行五个线程(我什至以毫秒为单位打印结果).........我有两个处理器,在程序中报告。

这怎么可能?

因为一个线程一次只能在一个CPU中运行,但是显示是5个线程同时运行。

有没有什么办法可以证明同一时间只能有一个线程在一个CPU上运行......

如果我像这样修改我的代码:

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

然后它显示如下输出:

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

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

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

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

子线程运行:1 99 Sun May 26 21:02:34 IST 2013

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

最佳答案

这取决于您所说的“同时”是什么意思。您可以通过切换在同一处理器上执行无限数量的线程,即从一个线程执行一行代码,然后切换到另一个线程,执行一行代码,然后切换回来。处理器通过非常快速地来回切换来模仿“同时执行”。

但是,大多数处理器在它们可以执行的真正同时线程的数量上受到它们拥有的内核数量的限制,但由于共享资源和硬件,即使这样也是一个糟糕的估计。理论上,您最多可以在 4 核处理器上同时运行 4 个线程。

关于java - 一个 CPU 上一次可以运行多少个线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16760689/

相关文章:

C Linux pthreads : sending data from one thread to antoher using message queue gives unexpected result

java - GWT 图像 setUrl()

java - Spring 3.1 DefaultHandlerExceptionResolver 覆盖/禁用

java - 如何在同一台显示器上打开一个帧并在 swing 中打开另一个帧?

c# - 如何 "Free"一个线程

windows - Windows 线程何时需要消息循环,为什么?

Java8 : how to copy values of selected fields from one object to other using lambda expression

java - 使用原子变量时的内存排序

c++ - 多线程建议

Python 杀死线程