「翻译」一篇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)
「翻译」一篇redis文章引发的翻译——JVM能支持多少线程?的更多相关文章
- Spring Boot 揭秘与实战(二) 数据存储篇 - Redis
文章目录 1. 环境依赖 2. 数据源 2.1. 方案一 使用 Spring Boot 默认配置 2.2. 方案二 手动创建 3. 使用 redisTemplate 操作4. 总结 3.1. 工具类 ...
- Linux 小知识翻译 - 「RAID」
最近术语「RAID」变得比较有名.「RAID」是指将多个HDD组合起来使用,从而提高存储可靠性的一种技术. 那么,关于 RAID 中的 「RAID 0」「RAID 1」「RAID 5」等各种「RAID ...
- 「翻译」Unity中的AssetBundle详解(二)
为AssetBundles准备资源 使用AssetBundles时,您可以随意将任何Asset分配给所需的任何Bundle.但是,在设置Bundles时,需要考虑一些策略.这些分组策略可以使用到任何你 ...
- 「翻译」Unity中的AssetBundle详解(一)
AssetBundles AssetBundle是一个存档文件,其中包含平台在运行时加载的特定资产(模型,纹理,预制,音频剪辑,甚至整个场景).AssetBundles可以表示彼此之间的依赖关系;例如 ...
- Linux 小知识翻译 - 「syslog」
这次聊聊「syslog」. 上次聊了「日志」(lgo).这次说起syslog,一看到log(日志)就明白是怎么回事了.syslog是获取系统日志的工具. 很多UINIX系的OS都采用了这个程序,它承担 ...
- Linux 小知识翻译 - 「Linux」怎么读?
主要讨论日语中的读法,所以没有完全按照原文来翻译. 「linux」的读法有很多(这里指在日语中),代表性的读法有以下几种: A). 李纳苦思 B). 李奴苦思 C). 纳依纳苦思 A和B相同的是将 l ...
- 翻译一篇英文文章,主要是给自己看的——在ASP.NET Core Web Api中如何刷新token
原文地址 :https://www.blinkingcaret.com/2018/05/30/refresh-tokens-in-asp-net-core-web-api/ 先申明,本人英语太菜,每次 ...
- Linux 小知识翻译 - 「日志」(log)
这次聊聊「日志」. 「日志」主要指系统或者软件留下的「记录」.出自表示「航海日志」的「logbook」. 经常听说「出现问题的时候,或者程序没有安装自己预期的来运行的时候,请看看日志!」. 确实,记录 ...
- Linux 小知识翻译 - 「RFC」
这次聊聊「RFC」. 有很多人经常听说「RFC」的吧,上次介绍的NTP是由「RFC1305规定的」,HTTP是由「RFC2616规定的」. RFC是「Request For Comments」的简称, ...
随机推荐
- Java三方---->excel框架之POI的使用一
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程序对Microsoft Office格式档案读和写的功能.pdf框架之IText的使用,参见我的博客:Java ...
- LeetCode——Product of Array Except Self
Description: Given an array of n integers where n > 1, nums, return an array output such that out ...
- navicat 激活流程
Navicat Premium 12激活 我自己测试了一下可以激活,很好用 原作链接:https://blog.csdn.net/loveer0/article/details/82016644 Na ...
- angular -- ng-ui-route路由及其传递参数?script标签版
考虑到 多视图等因素,所以 angular 的路由考虑使用 ng-ui-route来做,而不使用 ng-route来做! <!DOCTYPE html> <html lang=&qu ...
- create sequence
create sequence seq_test start with 3 increment by 1 minvalue 1 --范围-(1027 -1) maxvalue 99999999999 ...
- postgresql----字符串函数与操作符
函数 返回值类型 描述 示例 结果 string||string text 字符串连接 select 'Post'||'gresql'||' good!'; Postgresql good! stri ...
- ZOJ 3209 Treasure Map(精确覆盖)
Treasure Map Time Limit: 2 Seconds Memory Limit: 32768 KB Your boss once had got many copies of ...
- win10配置的静态/动态IP和 DNS的方法
1.配置静态IP和DNS netsh interface ip set address name="以太网" source=static addr=192.168.9.145 ma ...
- php中 const 与define()的区别 ,选择
来自: http://stackoverflow.com/questions/2447791/define-vs-const 相同点: 两者都可以定义常量 const FOO = 'BAR'; def ...
- | unauthenticated user (1130, "Host '127.0.0.1' is not allowed to connect to this MySQL server")
mysql> show processlist;+----+----------------------+-----------------+------+---------+------+-- ...