一个Java虚拟机可以支持多少线程?这因供应商而异吗?按操作系统?其他因素?


当前回答

当我在我的2GB三星AMD处理器笔记本电脑上运行Trisquel linux (Ubuntu 18.04)研究这个主题时。它可以管理9534个线程,然后抛出特殊的异常

在java.base / java.lang.Thread。start0(本地方法) 在java.base / java.lang.Thread.start (Thread.java: 803) 在Main.main (Main.java: 11)

代码:

public class MultithreadingRunnable implements Runnable {
    public void run() {
        System.out.println("ThreadID " +  Thread.currentThread().getId());
    }
 }



public class Main {
    public static void main(String[] ars) {

        for(int i = 0;i<10000;i++){
            Thread mr = new Thread(new MultithreadingRunnable());
            mr.start();
        }
    }
 }

其他回答

在阅读了Charlie Martin的文章后,我很好奇堆大小是否会对您可以创建的线程数量产生影响,结果完全让我目瞪口呆。

使用Vista Home Premium SP1上的JDK 1.6.0_11,我执行Charlie的测试应用程序,使用不同的堆大小,在2mb到1024mb之间。

例如,要创建一个2 MB的堆,我将使用参数-Xms2m -Xmx2m调用JVM。

以下是我的结果:

2 mb --> 5744 threads
4 mb --> 5743 threads
8 mb --> 5735 threads
12 mb --> 5724 threads
16 mb --> 5712 threads
24 mb --> 5687 threads
32 mb --> 5662 threads
48 mb --> 5610 threads
64 mb --> 5561 threads
96 mb --> 5457 threads
128 mb --> 5357 threads
192 mb --> 5190 threads
256 mb --> 5014 threads
384 mb --> 4606 threads
512 mb --> 4202 threads
768 mb --> 3388 threads
1024 mb --> 2583 threads

堆大小肯定很重要。但是堆大小和最大线程数之间的关系是反比的。

这很奇怪。

当我在我的2GB三星AMD处理器笔记本电脑上运行Trisquel linux (Ubuntu 18.04)研究这个主题时。它可以管理9534个线程,然后抛出特殊的异常

在java.base / java.lang.Thread。start0(本地方法) 在java.base / java.lang.Thread.start (Thread.java: 803) 在Main.main (Main.java: 11)

代码:

public class MultithreadingRunnable implements Runnable {
    public void run() {
        System.out.println("ThreadID " +  Thread.currentThread().getId());
    }
 }



public class Main {
    public static void main(String[] ars) {

        for(int i = 0;i<10000;i++){
            Thread mr = new Thread(new MultithreadingRunnable());
            mr.start();
        }
    }
 }

你可以处理任意数量的线程;没有限制。我在看电影和使用NetBeans时运行了下面的代码,它正常工作/没有停止机器。我认为你可以保留比这个程序更多的线程。

class A extends Thread {
    public void run() {
        System.out.println("**************started***************");
        for(double i = 0.0; i < 500000000000000000.0; i++) {
            System.gc();
            System.out.println(Thread.currentThread().getName());
        }
        System.out.println("************************finished********************************");
    }
}

public class Manager {
    public static void main(String[] args) {
        for(double j = 0.0; j < 50000000000.0; j++) {
            A a = new A();
            a.start();
        }
    }
}

嗯,很多。

这里有几个参数。特定的VM,加上VM上通常还有运行时参数。这在某种程度上是由操作系统驱动的:底层操作系统对线程有什么支持,它对线程有什么限制?如果虚拟机实际上使用操作系统级别的线程,那就是红色线程/绿色线程。

“支持”是另一个问题。如果你写一个Java程序,就像

   class DieLikeADog {
         public static void main(String[] argv){
             for(;;){
                new Thread(new SomeRunaable).start();
             }
         }
    }

(不要抱怨语法细节,这是我第一次喝咖啡),那么您肯定会有数百或数千个线程在运行。但是创建一个线程是相对昂贵的,调度器开销可能会变得紧张;不清楚是否可以让这些线程做任何有用的事情。

更新

好吧,忍不住了。下面是我的小测试程序,做了一些修饰:

public class DieLikeADog {
    private static Object s = new Object();
    private static int count = 0;
    public static void main(String[] argv){
        for(;;){
            new Thread(new Runnable(){
                    public void run(){
                        synchronized(s){
                            count += 1;
                            System.err.println("New thread #"+count);
                        }
                        for(;;){
                            try {
                                Thread.sleep(1000);
                            } catch (Exception e){
                                System.err.println(e);
                            }
                        }
                    }
                }).start();
        }
    }
}

在英特尔的OS/X 10.5.6和Java 6 5(见评论)上,这是我得到的

New thread #2547
New thread #2548
New thread #2549
Can't create thread: 5
New thread #2550
Exception in thread "main" java.lang.OutOfMemoryError: unable to create new native thread
        at java.lang.Thread.start0(Native Method)
        at java.lang.Thread.start(Thread.java:592)
        at DieLikeADog.main(DieLikeADog.java:6)

最大线程数取决于以下因素: 硬件配置,如微处理器,RAM。 操作系统是32位还是64位 run方法内部的代码。如果run方法内的代码很大,那么单个线程对象将有更多的内存需求