当正在执行的线程之一发生 stackoverflow 异常时,JVM 是否会退出?

发布于 2024-08-20 21:45:15 字数 37 浏览 6 评论 0原文

当正在执行的线程之一发生堆栈溢出异常时,JVM 是否会退出?

Does a JVM exit when a stack overflow exception occurs in one of the executing threads?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬シ愛 2024-08-27 21:45:15

您可以自己尝试使用,例如使用以下代码(生成并启动一个新线程并调用 a() ,它递归地调用自身以触发堆栈溢出,而另一个线程将某些内容打印到控制台):

public class SO {

    private static void a() {
        a();
    }

    public static void main(String[] args) throws InterruptedException {
    final Thread t = new Thread( new Runnable() {
        public void run() {
            a();
        }
    });
    t.start();
    while ( true ) {
        Thread.sleep( 2000 );
        System.out.println( "I'm still running @ " + System.currentTimeMillis() );
    }
}

您将看到堆栈溢出错误:

Exception in thread "Thread-1" java.lang.StackOverflowError

并且您还将看到打印线程继续愉快地打印。

另请注意,如果 EDT 线程终止,它会自动重新启动。

You can try it for yourself using, for example with the following code (a new thread is spawned and started and calls a() which calls itself recursively as to trigger a stack overflow while another thread prints something to the console):

public class SO {

    private static void a() {
        a();
    }

    public static void main(String[] args) throws InterruptedException {
    final Thread t = new Thread( new Runnable() {
        public void run() {
            a();
        }
    });
    t.start();
    while ( true ) {
        Thread.sleep( 2000 );
        System.out.println( "I'm still running @ " + System.currentTimeMillis() );
    }
}

You'll see your stack overflow error:

Exception in thread "Thread-1" java.lang.StackOverflowError

and you'll also see that the printing thread keeps happily printing along.

Also note that if the EDT thread dies, it is relaunched automagically.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文