StackOverflowError的JVM处理方式

背景:

事情来源于生产的一个异常日志
Caused by: java.lang.StackOverflowError: null at java.util.stream.Collectors.lambda$groupingBy$45(Collectors.java:908) at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169) at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
发生该异常并定位到是某个规则的问题后,我们手动修复了规则,不过我们无法确定是否需要重启容器,所以引申出来了发生StackOverflowError是否需要重启容器恢复的讨论

JVM对StackOverflowError线程的处理

结论是JVM只会中断发生StackOverflowError的线程,对于其他未发生StackOverflowError的线程没有影响,验证代码如下:

package stackoverflow;

/**
 * 验证JVM处理StackOverflowError的方式,只会中断异常线程,不影响主线程以及其他线程的执行
 */
public class StackOverFlowTest {
   


    public static void main(String[] args) throws Exception {
   
        Thread stackOverflowErrorThread = new Thread(new Runnable() {
   
            @Override
            public void run() {
   
                try {
   
                    Thread.sleep(60000L);
                } catch (InterruptedException e) {
   
                    e.printStackTrace();
                }
                // 进入死循环
                callRecursiveMethod();
            }
        }, "StackOverflowErrorThread");

        Thread otherNormalThread = new Thread(new Runnable() {
   
            @Override
            public void run() {
   
                while (true) {
   
                    System.out.println("I am otherNormalThread thread!!");
                    try {
   
                        Thread.sleep(10000L);
                    } catch (InterruptedException e) {
   
                        e.printStackTrace();
                    }
                }
            }
        }, "otherNormalThread");

        stackOverflowErrorThread.start();
        otherNormalThread.start();

        Thread.currentThread().setName("mainThread");
        while (true) {
   
            System.out.println("I am main thread!!");
            try {
   
                Thread.sleep(10000L);
            } catch (InterruptedException e) {
   
                e.printStackTrace();
            }
        }

    }

    /**
     * 死循环模拟StackOverflowError
     */
    public static void callRecursiveMethod() {
   
        callRecursiveMethod();
    }



}

未发生Stack Overflow之前
在这里插入图片描述
发生Stack Overflow之后
在这里插入图片描述

从输入日志也可以验证这一点:
在这里插入图片描述

参考:
https://blog.csdn.net/u011983531/article/details/79563162

相关推荐

  1. jvm 调优方式

    2023-12-28 12:44:02       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-28 12:44:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-28 12:44:02       106 阅读
  3. 在Django里面运行非项目文件

    2023-12-28 12:44:02       87 阅读
  4. Python语言-面向对象

    2023-12-28 12:44:02       96 阅读

热门阅读

  1. salesforce role和profile的区别

    2023-12-28 12:44:02       52 阅读
  2. 【网络协议】WebSocket知识点梳理和总结

    2023-12-28 12:44:02       48 阅读
  3. PyQt 打包成exe文件

    2023-12-28 12:44:02       67 阅读
  4. P8780 [蓝桥杯 2022 省 B] 刷题统计

    2023-12-28 12:44:02       54 阅读
  5. C动态内存分配与释放介绍

    2023-12-28 12:44:02       56 阅读
  6. SSH隧道远程连接局域网的电脑

    2023-12-28 12:44:02       63 阅读
  7. CF1895C

    2023-12-28 12:44:02       46 阅读
  8. 数字孪生在能源电力行业的技术难点和应用场景

    2023-12-28 12:44:02       58 阅读
  9. 设计模式_结构型模式_适配器模式

    2023-12-28 12:44:02       51 阅读