昨天看了一篇关于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. dialog里面数据更新问题

    在实际开发中经常会用到showDialog(int id)的方法来展示一个对话框,但是会遇到一个Dialog展示之后下次再show的时候对话框的界面还是上次展示的那个,而不是我们想象的界面.很多时候我 ...

  2. MQTT协议笔记之发布流程

    MQTT协议笔记之发布流程 前言 这次要讲到客户端/服务器的发布消息行为,与PUBLISH相关的消息类型,会在这里看到. PUBLISH 客户端发布消息经由服务器分发到所有对应的订阅者那里.一个订阅者 ...

  3. LeetCode——Pascal's Triangle II

    Description: Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3 ...

  4. ACM中Java高效输入输出封装

    来自互联网 : 既高效又好用才是王道! import java.io.IOException; import java.io.FileInputStream; import java.io.Input ...

  5. python epoll实现异步socket

    一.同步和异步: 在程序执行中,同步运行意味着等待调用的函数.线程.子进程等的返回结果后继续处理:异步指不等待当下的返回结果,直接运行主进程下面的程序,等到有返回结果时,通知主进程处理.有点高效. 二 ...

  6. jquery.js与sea.js综合使用

    jquery.js与sea.js综合使用   目录 模块定义 define id dependencies factory exports require require.async require. ...

  7. 【BZOJ3943】[Usaco2015 Feb]SuperBull 最大生成树

    [BZOJ3943][Usaco2015 Feb]SuperBull Description Bessie and her friends are playing hoofball in the an ...

  8. Python大数据:外部数据获取(网页抓取)

    import urllib2 as url import cookielib,StringIO,gzip,json import pandas as pd import numpy as np #定义 ...

  9. 【ArcGIS for SivlerLight api(3)】基础图层增删改查

    1.基础底图通常使用TiledLayer或者ArcGISDynamicLayer. 本质上都是在本地加载栅格图片.后台生成策略不同而已.从Vs2010的控件栏上拖过来的Map控件默认添加的底图是Esr ...

  10. Convolution and polynomial multiplication

    https://www.mathworks.com/help/matlab/ref/conv.html?s_tid=gn_loc_drop conv Convolution and polynomia ...