ITeye博客

Java大量线程注意点

pda158 · 2015-11-20 12:29 · Java学习 · 编程语言 · Markdown

-Xms64M -Xmx512M -Xss200K

  -Xms64M:设置JVM初始内存为64M。

  -Xmx512M:设置JVM最大可用内存512M。

  -Xss200K:设置单个线程的大小为200K。

  在程序中-Xss设置太小,可能程序会报错。默认设置是512K。但如果需要大量的线程,就可以将-Xss调小来获得更多的线程。

package com.competition.score.test;
import java.util.concurrent.CountDownLatch;
public class TestThread {
public static void main(String[] args) {
for (int i = 0;; i++) {
System.out.println("i = " + i);
new Thread(new HoldThread()).start();
}
}
}
class HoldThread extends Thread {
CountDownLatch cdl = new CountDownLatch(1);
public HoldThread() {
this.setDaemon(true);
}
public void run() {
try {
cdl.await();
} catch (InterruptedException e) {
}
}
}

 可用如上程序测试可用的最大线程数。注意:如上程序运行完毕需要重启虚拟机来清除线程占用。

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。