4

我正在开发一个 J2ME 项目,该项目为许多任务(例如下载 HTTP 内容)生成工作线程。基本的线程布局与大多数 Java 应用程序相似——有一个主 UI 线程和工作线程来执行幕后工作。我的问题是处理工作线程中发生的异常的最佳方法是什么?

我通常坚持大多数异常应该尽可能渗透的设计原理。当我编写单线程应用程序时,我通常会将异常一直渗透到 UI 层,然后在错误对话框中将它们报告给用户。多线程应用程序是否有类似的做法?对我来说最直观的是在 Thread.run() 中捕获异常,然后在 UI 线程上调用 invokeLater 以在对话框中报告它。我在这里看到的问题是,在工作线程过早死亡之外,这种方法并没有真正通知 UI 线程有错误。可以这么说,我看不到跨线程抛出异常的明确方法。

谢谢,安迪

4

2 回答 2

7

你不应该把 UI 代码塞进你的工人!

/**
 * TWO CHOICES:
 * - Monitor your threads and report errors,
 * - setup a callback to do something.
 */
public class ThreadExceptions {

    /** Demo of {@link RunnableCatch} */
    public static void main(String[] argv) throws InterruptedException {
        final Runnable bad = new NaughtyThread();
        // safe1 doesnt have a callback
        final RunnableCatch safe1 = new RunnableCatch(bad);
        // safe2 DOES have a callback
        final RunnableCatch safe2 = new RunnableCatch(bad, new RunnableCallback() {
            public void handleException(Runnable runnable, Exception exception) {
                System.out.println("Callback handled: " + exception.getMessage());
                exception.printStackTrace();
            }

        });
        final Thread t1 = new Thread(safe1, "myThread");
        final Thread t2 = new Thread(safe2, "myThread");
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        if (safe1.getException() != null) {
            System.out.println("thread finished with exceptions");
            safe1.getException().printStackTrace();
        }
        System.out.println("done");
    }


}

/** Throws an exception 50% of the time */
class NaughtyThread implements Runnable {
    public void run() {
        try {
            if (Math.random() > .5) {
                throw new RuntimeException("badness");
            }
        } finally {
            System.out.println("ran");
        }
    }
}

/** Called when an exception occurs */
interface RunnableCallback {
    void handleException(Runnable runnable, Exception exception);
}

/**
 * Catches exceptions thrown by a Runnable,
 * so you can check/view them later and/or
 * deal with them from some callback.
 */
class RunnableCatch implements Runnable {

    /** Proxy we will run */
    private final Runnable _proxy;

    /** Callback, if any */
    private final RunnableCallback _callback;

    /** @guarded-by(this) */
    private Exception _exception;

    public RunnableCatch(final Runnable proxy) {
        this(proxy, null);
    }

    public RunnableCatch(final Runnable proxy, RunnableCallback target) {
        _proxy = proxy;
        _callback = target;
    }

    public void run() {
        try {
            _proxy.run();
        } catch (Exception e) {
            synchronized (this) {
                _exception = e;
            }
            if (_callback != null) {
                _callback.handleException(_proxy, e);
            }
        }
    }

    /** @return any exception that occured, or NULL */
    public synchronized Exception getException() {
        return _exception;
    }
}
于 2008-11-16T04:17:20.350 回答
0

除了 Stuph 给出的选项之外,另一个选项是在本地线程中设置异常。如果在清除该异常之前发生另一个异常,则发生断言。这至少让某人有机会注意到异常并对其进行处理。

于 2008-11-16T04:28:55.193 回答