---
title: "Java大量线程注意点"
author: "pda158"
date: "2015-11-20T12:29:36Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2015-11-20T12:29:36Z"
id: 2258075
url: "https://pda158.iteye.com/blog/2258075"
---

-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) {
}
}
}
```

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