「翻译」一篇redis文章引发的翻译——JVM能支持多少线程?

昨天看了一篇关于redis 的文。

https://www.cnblogs.com/fanwencong/p/5782860.html

作者说他模拟了100万线程的并发,我对这个有一些怀疑,看了评论也有很多质疑的声音。当然我这篇不是要批评作者对线程的模拟,事实上作者写的对redis的使用是很不错的,我们本篇主要针对个人电脑上的JVM最多能支持多少个线程。以下是StackOverflow上的一个提问,我简单的翻译了一下。
StackOverflow原回答请点我


Eddie 的回答。

This depends on the CPU you’re using, on the OS, on what other processes are doing, on what Java release you’re using, and other factors. I’ve seen a WINDOWS server have > 6500 Threads before bringing the machine down. Most of the threads were not doing anything, of course. Once the machine hit around 6500 Threads (in Java), the whole machine started to have problems and become unstable.My experience shows that Java (recent versions) can happily consume as many Threads as the computer itself can host without problems.Of course, you have to have enough RAM and you have to have started Java with enough memory to do everything that the Threads are doing and to have a stack for each Thread. Any machine with a modern CPU (most recent couple generations of AMD or Intel) and with 1 - 2 Gig of memory (depending on OS) can easily support a JVM with thousands of Threads.If you need a more specific answer than this, your best bet is to profile.

译:这取决于你使用的CPU、操作系统、取决于其他进程在处理什么事情、你使用的Java版本和其他因素。我曾经见过一个WINDOWS 服务器,它上面有超过6500个线程。当然大多数线程是空闲的。一旦电脑上达到6500个线程(在Java中),整个机器开始出现问题并且变得不稳定。我的经验是无论计算机本身能够维持多少线程,Java(最近版本的)都能吃得下,并且不出问题。当然你需要足够的RAM并且你必须用足够的内存来启动Java来满足这么多线程的需要,并且需要有一个栈来存储线程。任何使用现代CPU的计算机(最近两代的AMD或Intel)和1-2G的内存(取决于操作系统)就可以支持JVM创建上千个线程。如果你需要比这个回答更具体的答案,最好的选择是看一下相关的文档。

Charlie Martin 的回答

Um, lots.

There are several parameters here. The specific VM, plus there are usually run-time parameters on the VM as well. That’s somewhat driven by the operating system: what support does the underlying OS have for threads and what limitations does it put on them? If the VM actually uses OS-level threads at all, the good old red thread/green thread thing.

What “support” means is another question. If you write a Java program that is just something like

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

(and don’t complain about little syntax details, I’m on my first cup of coffee) then you should certainly expect to get hundreds or thousands of threads running. But creating a Thread is relatively expensive, and scheduler overhead can get intense; it’s unclear that you could have those threads do anything useful.

Update

Okay, couldn’t resist. Here’s my little test program, with a couple embellishments:

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();
        }
    }
}

On OS/X 10.5.6 on Intel, and Java 6 5 (see comments), here’s what I got

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)

翻译:

呃,很多。

What “support” means is another question. If you write a Java program that is just something like

有很多种情况。特定的虚拟机,特定VM,加上VM上通常还有运行时参数。跟操作系统也有点关系:运行的操作系统对多线程有怎样的支持和对线程的有何种限制?如果VM完全使用操作系统级的线程,那即是红线程/绿色线程的事情。(译:这一句不太懂)

何种“支持”意味着另一种问题。如果你写一个Java程序,就像这样

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

(先不要抱怨语法上的细节,我在喝今天的第一杯咖啡),你应该期望创建成百上千的运行线程。但是创建一个线程是很昂贵的,调度器开销也会变得很紧张。不清楚你用这些线程可以做什么有用的事情。

Update

Okay, couldn’t resist. Here’s my little test program, with a couple embellishments:

好吧,受不了反驳了。我给出一个简单的测试程序。

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 on Intel, and 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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值