12

(下面的示例代码是独立且可运行的,您可以尝试一下,它不会使您的系统崩溃:)

Tom Hawtin 在这里评论了这个问题:Why do people run Java GUI's on the Event Queue

那:

EDT 不太可能崩溃。在 EDT 调度中抛出的未经检查的异常被捕获、转储并且线程继续。

有人可以解释一下这里发生了什么(每次单击“抛出未经检查的异常”按钮时,都会故意除以零):

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class CrashEDT extends JFrame {

    public static void main(String[] args) {
        final CrashEDT frame = new CrashEDT();
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing( WindowEvent e) {
                System.exit(0);
            }
        });
        final JButton jb = new JButton( "throw an unchecked exception" );
        jb.addActionListener( new ActionListener() {
            public void actionPerformed( ActionEvent e ) {
                System.out.println( "Thread ID:" + Thread.currentThread().getId() );
                System.out.println( 0 / Math.abs(0) );
            }
        } );
        frame.add( jb );
        frame.setSize(300, 150);
        frame.setVisible(true);
    }

}

我收到以下消息(这是我所期望的):

Exception in thread "AWT-EventQueue-0" java.lang.ArithmeticException: / by zero

对我来说,这是一个未经检查的例外,对吧?

您可以看到每次触发崩溃时线程 ID 都会增加。

那么,EDT 是否会在每次引发未经检查的异常时自动重新启动,或者像 Tom Hawtin 评论的那样,未经检查的异常“被捕获、转储并且线程继续” ?

这里发生了什么?

4

3 回答 3

5

有趣的问题。我会认为异常被捕获并且线程继续,但经过一些研究我不太确定。

我用一个扩展了你的程序

Set<Thread> seenAwtThreads = new HashSet<Thread>();

其中我收集了所有“看到的”awt 线程,每次单击“抛出异常”按钮时,集合的大小都会增加,这似乎表明在发生异常时会初始化一个新线程。

run最后我在实现中找到了这个评论EventDispatchThread

/*
 * Event dispatch thread dies in case of an uncaught exception. 
 * A new event dispatch thread for this queue will be started
 * only if a new event is posted to it. In case if no more
 * events are posted after this thread died all events that 
 * currently are in the queue will never be dispatched.
 */

完整的 run 方法的实现如下所示:

public void run() {
    try {
        pumpEvents(new Conditional() {
            public boolean evaluate() {
                return true;
            }
        });     
    } finally {
        /*
         * This synchronized block is to secure that the event dispatch 
         * thread won't die in the middle of posting a new event to the
         * associated event queue. It is important because we notify
         * that the event dispatch thread is busy after posting a new event
         * to its queue, so the EventQueue.dispatchThread reference must
         * be valid at that point.
         */
        synchronized (theQueue) {
            if (theQueue.getDispatchThread() == this) {
                theQueue.detachDispatchThread();
            }
            /*
             * Event dispatch thread dies in case of an uncaught exception. 
             * A new event dispatch thread for this queue will be started
             * only if a new event is posted to it. In case if no more
             * events are posted after this thread died all events that 
             * currently are in the queue will never be dispatched.
             */
            /*
             * Fix for 4648733. Check both the associated java event
             * queue and the PostEventQueue.
             */
            if (theQueue.peekEvent() != null || 
                !SunToolkit.isPostEventQueueEmpty()) { 
                theQueue.initDispatchThread();
            }
            AWTAutoShutdown.getInstance().notifyThreadFree(this);
        }
    }
}
于 2010-06-11T07:17:01.087 回答
4

作为参考,“该机器的特定行为取决于实现。” 例如,线程 ID 在我的平台上保持不变。在AWT 线程问题中讨论的最终效果是“当至少有一个可显示的组件时,JVM 不会退出”。

于 2010-06-11T07:28:55.003 回答
0

在 Event Dispatch Thread 上设置了一个默认的UncaughtExceptionHandler,它将异常打印到 System.out,然后在 Thread 中继续。

于 2011-02-02T18:33:38.040 回答