我在开放的VZ虚拟化服务器上运行linux Ubuntu 16.04.4 LTS,并遇到以下问题:在得到OutOfMemoryError之前,我无法在Java中启动更多的1000个线程。 我写了一个小程序来测试最大线程数
private static Object s = new Object();
private static int count = 0;
public static void main(String[] argv){
for(;;){
try {
new Thread(new Runnable(){
public void run(){
synchronized(s){
count += 1;
if(count % 1000 == 0) {
System.err.println("New thread #"+count);
}
}
for(;;){
try {
Thread.sleep(1000);
} catch (Exception e){
System.err.println(e);
}
}
}
}).start();
}catch (OutOfMemoryError e) {
System.out.println("Unable to create new native thread current threads: " + count);
System.exit(0);
}
}
}
每次运行时,它都会在1010至1040个线程之间停止,即使我的Windows PC可以处理100000个线程,Linux中的问题在哪里?任何人都有这个主意吗?我的用户限制是:
core file size (blocks, -c) 0
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 1547020
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 99999
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 10240
cpu time (seconds, -t) unlimited
max user processes (-u) 99999
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
编辑: 同样,如果user1运行500个线程,这也不取决于用户,user2只能启动500个以上