JVM如何处理StackOverflowError

JVM如何处理StackOverflowError如何模拟StackOverflowError,可以参考:http://blog.csdn.net/u011983531/article/details/63250882我们知道,当虚拟机栈容量太小或者栈帧太大时,程序都会抛出StackOverflowError异常,对于StackOverflowError的处理方法,有下面两种:通过try…catch(Throwable…

如何模拟StackOverflowError,可以参考:
http://blog.csdn.net/u011983531/article/details/63250882

我们知道,当虚拟机栈容量太小或者栈帧太大时,程序都会抛出StackOverflowError异常,对于StackOverflowError的处理方法,有下面两种:

  1. 通过try…catch(Throwable t){}来捕获,然后跟普通异常一样,对StackOverflowError进行处理
  2. 不捕获,然后交给JVM处理,JVM此时会中断当前线程,但是不会影响其他线程,因为虚拟机栈是每个线程私有的

下面,我们分别来模拟一下两种方法:

1.捕获StackOverflowError

public class SOFTest { 
   

    public static void main(String[] args) {
        new Thread(new SOFClass(),"Sub thread").start();
        while (true){
            System.out.println("I am main thread!");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static class SOFClass implements Runnable{ 
   

        @Override
        public void run() {
            try {
                recursiveMethod();
            }catch (Throwable throwable){
                System.out.println("捕获到了异常:"+ throwable.getClass());
            }
            while (true){
                System.out.println("I am sub thread!");
                try {
                    Thread.sleep(1000L);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }

        public void recursiveMethod(){
            recursiveMethod();
        }
    }
}

//运行结果
I am main thread!
捕获到了异常:class java.lang.StackOverflowError
I am sub thread!
I am main thread!
I am sub thread!
I am main thread!
I am sub thread!
I am main thread!
I am sub thread!

2.JVM处理StackOverflowError

下面,我们不捕获StackOverflowError异常,当我们没有捕获时,异常就交给了JVM去处理了。

public class SOFTest {

    public static void main(String[] args) {
        new Thread(new SOFClass(),"Sub thread").start();
        while (true){
            System.out.println("I am main thread!");
            try {
                Thread.sleep(1000L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private static class SOFClass implements Runnable{

        @Override
        public void run() {
            recursiveMethod();
       }

        public void recursiveMethod(){
            recursiveMethod();
        }
    }
}

//运行结果
I am main thread!
Exception in thread "Sub thread" java.lang.StackOverflowError
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    at com.arch.test.jvm.SOFTest$SOFClass.recursiveMethod(SOFTest.java:37)
    ……
I am main thread!
I am main thread!
I am main thread!
I am main thread!
I am main thread!
……

我们可以看到,JVM中断了Sub thread,但是主线程仍然在运行,没有影响

今天的文章JVM如何处理StackOverflowError分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/12361.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注