Time to put a stop to Thread.stop?

Martin Buchholz martinrb at google.com
Fri May 24 18:14:43 UTC 2013


On Fri, May 24, 2013 at 6:18 AM, Alan Bateman <Alan.Bateman at oracle.com>wrote:

>
> The webrev with the proposed changes is here. As I mentioned in one of the
> replies, there are 4 j.u.c tests that need to be updated so I've changed
> these tests to use Unsafe.throwException.
>

Alan, you're telling everyone there's no need for Thread.stop, but you are
replacing usages in tests with calls to Unsafe, which is not available to
normal code.  So you have a kind of moral obligation here to replace usages
with "ordinary" java code.  There are other ways to do sneakyThrow, and
perhaps a sneakyRethrow method should be added to the jdk test library.

Ordinary java code should be able to simply catch and rethrow a Throwable
if type analysis can "prove" that the exception is not an Exception.

As a data point, Doug uses this:

(Doug, it's not obvious to me why you handle Error and RuntimeException
specially)

    /**
     * A version of "sneaky throw" to relay exceptions
     */
    static void rethrow(final Throwable ex) {
        if (ex != null) {
            if (ex instanceof Error)
                throw (Error)ex;
            if (ex instanceof RuntimeException)
                throw (RuntimeException)ex;
            ForkJoinTask.<RuntimeException>uncheckedThrow(ex);
        }
    }

    /**
     * The sneaky part of sneaky throw, relying on generics
     * limitations to evade compiler complaints about rethrowing
     * unchecked exceptions
     */
    @SuppressWarnings("unchecked") static <T extends Throwable>
        void uncheckedThrow(Throwable t) throws T {
        if (t != null)
            throw (T)t; // rely on vacuous cast
    }



More information about the core-libs-dev mailing list