昨天看了一篇关于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)

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

  1. Spring Boot 揭秘与实战(二) 数据存储篇 - Redis

    文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用 redisTemplate 操作4. 总结 3.1. 工具类 ...

  2. Linux 小知识翻译 - 「RAID」

    最近术语「RAID」变得比较有名.「RAID」是指将多个HDD组合起来使用,从而提高存储可靠性的一种技术. 那么,关于 RAID 中的 「RAID 0」「RAID 1」「RAID 5」等各种「RAID ...

  3. 「翻译」Unity中的AssetBundle详解(二)

    为AssetBundles准备资源 使用AssetBundles时,您可以随意将任何Asset分配给所需的任何Bundle.但是,在设置Bundles时,需要考虑一些策略.这些分组策略可以使用到任何你 ...

  4. 「翻译」Unity中的AssetBundle详解(一)

    AssetBundles AssetBundle是一个存档文件,其中包含平台在运行时加载的特定资产(模型,纹理,预制,音频剪辑,甚至整个场景).AssetBundles可以表示彼此之间的依赖关系;例如 ...

  5. Linux 小知识翻译 - 「syslog」

    这次聊聊「syslog」. 上次聊了「日志」(lgo).这次说起syslog,一看到log(日志)就明白是怎么回事了.syslog是获取系统日志的工具. 很多UINIX系的OS都采用了这个程序,它承担 ...

  6. Linux 小知识翻译 - 「Linux」怎么读?

    主要讨论日语中的读法,所以没有完全按照原文来翻译. 「linux」的读法有很多(这里指在日语中),代表性的读法有以下几种: A). 李纳苦思 B). 李奴苦思 C). 纳依纳苦思 A和B相同的是将 l ...

  7. 翻译一篇英文文章,主要是给自己看的——在ASP.NET Core Web Api中如何刷新token

    原文地址 :https://www.blinkingcaret.com/2018/05/30/refresh-tokens-in-asp-net-core-web-api/ 先申明,本人英语太菜,每次 ...

  8. Linux 小知识翻译 - 「日志」(log)

    这次聊聊「日志」. 「日志」主要指系统或者软件留下的「记录」.出自表示「航海日志」的「logbook」. 经常听说「出现问题的时候,或者程序没有安装自己预期的来运行的时候,请看看日志!」. 确实,记录 ...

  9. Linux 小知识翻译 - 「RFC」

    这次聊聊「RFC」. 有很多人经常听说「RFC」的吧,上次介绍的NTP是由「RFC1305规定的」,HTTP是由「RFC2616规定的」. RFC是「Request For Comments」的简称, ...

随机推荐

  1. 什么是"抓包"?怎样"抓包"?

    你是网络管理员吗?你是不是有过这样的经历:在某一天的早上你突然发现网络性能急剧下降,网络服务不能正常提供,服务器访问速度极慢甚至不能访问,网络交换机端口指示灯疯狂地闪烁.网络出口处的路由器已经处于满负 ...

  2. mysql字符串根据指定字符分割

    1.分割函数:SUBSTRING_INDEX('浙江温州-中国电信','-','1') 2.用例(筛选'-'前至少4个汉字的数据) a.数据分布 b.筛选sql select t.mobile_num ...

  3. Linux系统下Nginx+PHP 环境安装配置

    一.编译安装Nginx 官网:http://wiki.nginx.org/Install 下载:http://nginx.org/en/download.html # tar -zvxf nginx- ...

  4. 基于JDK1.8的String源码学习笔记

    String,可能是学习Java一上来就学习的,经常用,但是却往往只是一知半解,甚至API有时也得现查.所以还是老规矩,倒腾源码. 一.java doc 这次首先关注String的doc,因为其实作为 ...

  5. Oracle之rman命令的使用(51CTO风哥rman课程)

    看rman的连接串的帮助 连接数据库 rman target/ rman的版本要和目标数据库一致(一般大版本可以往下兼容小版本) 运行操作系统命令 run {host "pwd"; ...

  6. Oracle数据类型clob和blob

    clob(size)变长 字符型大对象,最大8tb 应用场景:如果varchar2不够用了使用clob blob(size)变长 二进制数据,可以存放图片声音 8tb 但是,实际工作中很少把图片和声音 ...

  7. pta 习题集5-6 堆栈操作合法性

    假设以S和X分别表示入栈和出栈操作.如果根据一个仅由S和X构成的序列,对一个空堆栈进行操作,相应操作均可行(如没有出现删除时栈空)且最后状态也是栈空,则称该序列是合法的堆栈操作序列.请编写程序,输入S ...

  8. Redundant Paths---poj3177(双连通分量)

    题目链接:http://poj.org/problem?id=3177 题意:有n个牧场,Bessie 要从一个牧场到另一个牧场,要求至少要有2条独立的路可以走.现已有m条路,求至少要新建多少条路,使 ...

  9. 过山车---hdu2063(最大匹配)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2063最大匹配模板题: #include <iostream> #include <c ...

  10. (2.3)DDL增强功能-流程化控制与动态sql

    1.流程控制 在T-SQL中,与流程控制语句相关的关键字有8个: BEGIN...END BREAK GOTO CONTINUE IF...ELSE WHILE RETURN WAITFOR 其实还可 ...