From 4f9533c7722fa07511a94d005227961f4a4dec23 Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Thu, 18 May 2006 17:29:21 +0000 Subject: Imported GNU Classpath 0.90 Imported GNU Classpath 0.90 * scripts/makemake.tcl: LocaleData.java moved to gnu/java/locale. * sources.am: Regenerated. * gcj/javaprims.h: Regenerated. * Makefile.in: Regenerated. * gcj/Makefile.in: Regenerated. * include/Makefile.in: Regenerated. * testsuite/Makefile.in: Regenerated. * gnu/java/lang/VMInstrumentationImpl.java: New override. * gnu/java/net/local/LocalSocketImpl.java: Likewise. * gnu/classpath/jdwp/VMMethod.java: Likewise. * gnu/classpath/jdwp/VMVirtualMachine.java: Update to latest interface. * java/lang/Thread.java: Add UncaughtExceptionHandler. * java/lang/reflect/Method.java: Implements GenericDeclaration and isSynthetic(), * java/lang/reflect/Field.java: Likewise. * java/lang/reflect/Constructor.java * java/lang/Class.java: Implements Type, GenericDeclaration, getSimpleName() and getEnclosing*() methods. * java/lang/Class.h: Add new public methods. * java/lang/Math.java: Add signum(), ulp() and log10(). * java/lang/natMath.cc (log10): New function. * java/security/VMSecureRandom.java: New override. * java/util/logging/Logger.java: Updated to latest classpath version. * java/util/logging/LogManager.java: New override. From-SVN: r113887 --- libjava/java/lang/Thread.java | 149 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 149 insertions(+) (limited to 'libjava/java/lang/Thread.java') diff --git a/libjava/java/lang/Thread.java b/libjava/java/lang/Thread.java index 7938498..505b99b 100644 --- a/libjava/java/lang/Thread.java +++ b/libjava/java/lang/Thread.java @@ -127,11 +127,17 @@ public class Thread implements Runnable /** The context classloader for this Thread. */ private ClassLoader contextClassLoader; + /** The default exception handler. */ + private static UncaughtExceptionHandler defaultHandler; + /** Thread local storage. Package accessible for use by * InheritableThreadLocal. */ WeakIdentityHashMap locals; + /** The uncaught exception handler. */ + UncaughtExceptionHandler exceptionHandler; + // This describes the top-most interpreter frame for this thread. RawData interp_frame; @@ -935,4 +941,147 @@ public class Thread implements Runnable } return locals; } + + /** + * Assigns the given UncaughtExceptionHandler to this + * thread. This will then be called if the thread terminates due + * to an uncaught exception, pre-empting that of the + * ThreadGroup. + * + * @param h the handler to use for this thread. + * @throws SecurityException if the current thread can't modify this thread. + * @since 1.5 + */ + public void setUncaughtExceptionHandler(UncaughtExceptionHandler h) + { + SecurityManager sm = SecurityManager.current; // Be thread-safe. + if (sm != null) + sm.checkAccess(this); + exceptionHandler = h; + } + + /** + *

+ * Returns the handler used when this thread terminates due to an + * uncaught exception. The handler used is determined by the following: + *

+ * + * + * @return the appropriate UncaughtExceptionHandler or + * null if one can't be obtained. + * @since 1.5 + */ + public UncaughtExceptionHandler getUncaughtExceptionHandler() + { + return exceptionHandler != null ? exceptionHandler : group; + } + + /** + *

+ * Sets the default uncaught exception handler used when one isn't + * provided by the thread or its associated ThreadGroup. + * This exception handler is used when the thread itself does not + * have an exception handler, and the thread's ThreadGroup + * does not override this default mechanism with its own. As the group + * calls this handler by default, this exception handler should not defer + * to that of the group, as it may lead to infinite recursion. + *

+ *

+ * Uncaught exception handlers are used when a thread terminates due to + * an uncaught exception. Replacing this handler allows default code to + * be put in place for all threads in order to handle this eventuality. + *

+ * + * @param h the new default uncaught exception handler to use. + * @throws SecurityException if a security manager is present and + * disallows the runtime permission + * "setDefaultUncaughtExceptionHandler". + * @since 1.5 + */ + public static void + setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler h) + { + SecurityManager sm = SecurityManager.current; // Be thread-safe. + if (sm != null) + sm.checkPermission(new RuntimePermission("setDefaultUncaughtExceptionHandler")); + defaultHandler = h; + } + + /** + * Returns the handler used by default when a thread terminates + * unexpectedly due to an exception, or null if one doesn't + * exist. + * + * @return the default uncaught exception handler. + * @since 1.5 + */ + public static UncaughtExceptionHandler getDefaultUncaughtExceptionHandler() + { + return defaultHandler; + } + + /** + *

+ * This interface is used to handle uncaught exceptions + * which cause a Thread to terminate. When + * a thread, t, is about to terminate due to an uncaught + * exception, the virtual machine looks for a class which + * implements this interface, in order to supply it with + * the dying thread and its uncaught exception. + *

+ *

+ * The virtual machine makes two attempts to find an + * appropriate handler for the uncaught exception, in + * the following order: + *

+ *
    + *
  1. + * t.getUncaughtExceptionHandler() -- + * the dying thread is queried first for a handler + * specific to that thread. + *
  2. + *
  3. + * t.getThreadGroup() -- + * the thread group of the dying thread is used to + * handle the exception. If the thread group has + * no special requirements for handling the exception, + * it may simply forward it on to + * Thread.getDefaultUncaughtExceptionHandler(), + * the default handler, which is used as a last resort. + *
  4. + *
+ *

+ * The first handler found is the one used to handle + * the uncaught exception. + *

+ * + * @author Tom Tromey + * @author Andrew John Hughes + * @since 1.5 + * @see Thread#getUncaughtExceptionHandler() + * @see Thread#setUncaughtExceptionHander(java.lang.Thread.UncaughtExceptionHandler) + * @see Thread#getDefaultUncaughtExceptionHandler() + * @see + * Thread#setDefaultUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) + */ + public interface UncaughtExceptionHandler + { + /** + * Invoked by the virtual machine with the dying thread + * and the uncaught exception. Any exceptions thrown + * by this method are simply ignored by the virtual + * machine. + * + * @param thr the dying thread. + * @param exc the uncaught exception. + */ + void uncaughtException(Thread thr, Throwable exc); + } } -- cgit v1.1