一 代码
package concurrent;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
public class ThreadCounter extends Thread {
final static AtomicInteger counter = new AtomicInteger(0);
public static void main(String[] args) {
try {
while (true) {
new ThreadCounter().start();
}
} catch (Exception e) {
System.out.println("failed at =>" + counter.get());
}
}
@Override
public void run() {
try {
System.out.println("The " + counter.getAndIncrement() + " thread be created.");
TimeUnit.MINUTES.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
二 测试
测试脚本
E:\JVMDemo\demo2022\target\classes>java -Xmx512m -Xms64m concurrent.ThreadCounter