昨天看了一篇关于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. MyBatis官方文档——入门

    入门 安装 要使用 MyBatis, 只需将 mybatis-x.x.x.jar 文件置于 classpath 中即可. 如果使用 Maven 来构建项目,则需将下面的 dependency 代码置于 ...

  2. 使用FileZilla向linux系统上传文件

  3. 【BZOJ3312】[Usaco2013 Nov]No Change 状压DP+二分

    [BZOJ3312][Usaco2013 Nov]No Change Description Farmer John is at the market to purchase supplies for ...

  4. 微软官方:SELECT语句逻辑处理顺序

    以下步骤显示SELECT 语句的逻辑处理顺序或绑定顺序.此顺序确定在一个步骤中定义的对象何时可用于后续步骤中的子句. 例如,如果查询处理器可以绑定到(访问)在FROM 子句中定义的表或视图,则这些对象 ...

  5. CHECKSUM比较两表字段值差异

    CHECKSUM 返回在表的行上或在表达式列表上计算的校验值.CHECKSUM 用于生成哈希索引. 语法 CHECKSUM ( * | expression [ ,...n ] ) 参数 * 指定在表 ...

  6. SQL中 decode()函数简介(转载)

    今天看别人的SQL时看这里面还有decode()函数,以前从来没接触到,上网查了一下,还挺好用的一个函数,写下来希望对朋友们有帮助哈! decode()函数简介: 主要作用:将查询结果翻译成其他值(即 ...

  7. zabbix debug and vulnerability https://www.zabbix.com/documentation/3.0/manual/concepts/sender

    https://www.zabbix.com/documentation/3.0/manual/concepts/sender zabbix--- zabbix_sender -vv -z 172.2 ...

  8. Android之上下文context

    Context,中文直译为“上下文”,SDK中对其说明如下: 1.它描述的是一个应用程序环境的信息,即上下文. 2.该类是一个抽象(abstract class)类,Android提供了该抽象类的具体 ...

  9. 洛谷 P2602 [ZJOI2010]数字计数

    洛谷 第一次找规律A了一道紫题,写篇博客纪念一下. 这题很明显是数位dp,但是身为蒟蒻我不会呀,于是就像分块打表水过去. 数据范围是\(10^{12}\),我就\(10^6\)一百万一百万的打表. 于 ...

  10. python获取当天日期进行格式转换

    # Python Library import time def getToday(format=3): """返回今天的日期字串""" # ...