一个线程OOM后,其他线程还能运行吗? 可以正常运行 ,测试用例如下:
public class TsetUtil {
public static void main(String[] args) {
// 线程1 (此thread会OOM 堆溢出)
Thread thread = new Thread(){
@Override
public void run(){
System.out.println("Thread Running");
List<byte[]> list = new ArrayList<byte[]>();
while (true) {
System.out.println(new Date().toString() + Thread.currentThread() + "1==");
byte[] b = new byte[100*1024 * 1024 * 1];
list.add(b);
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
thread.start();
// 线程2 (此thread会OOM 栈溢出)
Thread thread2 = new Thread(){
@Override
public void run(){
//System.out.println("Thread Running");
StackTest test=new StackTest();
test.Test();
}
};
thread2.start();
// 线程2 (正常运行)
Thread thread3 = new Thread(){
@Override
public void run(){
while (true) {
System.out.println(new Date().toString() + Thread.currentThread() + "2==");
try {
Thread.sleep(1000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
thread3.start();
}
//栈深度大于虚拟机所允许的最大深度时发生栈溢出的测试
static class StackTest
{
static long num=0;
public void Test()
{
num++;
System.out.println("调用栈"+num+"次");
this.Test();
}
}
}
控制台输出如下:
调用栈1次
Thu Apr 08 13:45:20 CST 2021Thread[Thread-0,5,main]1==
调用栈.........
调用栈6324次
调用栈6325次
Exception in thread "Thread-1" java.lang.StackOverflowError //此处线程出现栈溢出
Thu Apr 08 13:45:31 CST 2021Thread[Thread-2,5,main]2==
Thu Apr 08 13:45:32 CST 2021Thread[Thread-0,5,main]1==
Thu Apr 08 13:45:32 CST 2021Thread[Thread-2,5,main]2==
Thu Apr 08 13:45:33 CST 2021Thread[Thread-0,5,main]1==
Thu Apr 08 13:45:33 CST 2021Thread[Thread-2,5,main]2==
Thu Apr 08 13:45:34 CST 2021Thread[Thread-0,5,main]1==
Exception in thread "Thread-0" java.lang.OutOfMemoryError: Java heap space //此处线程出现堆溢出
at com.kpcq.framework.utils.TsetUtil$1.run(TsetUtil.java:26)
Thu Apr 08 13:45:34 CST 2021Thread[Thread-2,5,main]2==
Thu Apr 08 13:45:35 CST 2021Thread[Thread-2,5,main]2== //其它线程继续运行
Thu Apr 08 13:45:36 CST 2021Thread[Thread-2,5,main]2==
原文:https://www.cnblogs.com/zt2710/p/14631844.html