Merge
authoralanb
Tue, 11 Jun 2013 14:09:06 +0100
changeset 18161 d9558f046caa
parent 18160 da854405dc59 (diff)
parent 18158 d5a620310f97 (current diff)
child 18162 25020bbe1dd5
child 19045 bc9a25fff6c5
Merge
--- a/jdk/src/share/classes/java/lang/Thread.java	Tue Jun 11 12:13:26 2013 +0200
+++ b/jdk/src/share/classes/java/lang/Thread.java	Tue Jun 11 14:09:06 2013 +0100
@@ -812,68 +812,10 @@
      */
     @Deprecated
     public final void stop() {
-        stop(new ThreadDeath());
-    }
-
-    /**
-     * Forces the thread to stop executing.
-     * <p>
-     * If there is a security manager installed, the <code>checkAccess</code>
-     * method of this thread is called, which may result in a
-     * <code>SecurityException</code> being raised (in the current thread).
-     * <p>
-     * If this thread is different from the current thread (that is, the current
-     * thread is trying to stop a thread other than itself) or
-     * <code>obj</code> is not an instance of <code>ThreadDeath</code>, the
-     * security manager's <code>checkPermission</code> method (with the
-     * <code>RuntimePermission("stopThread")</code> argument) is called in
-     * addition.
-     * Again, this may result in throwing a
-     * <code>SecurityException</code> (in the current thread).
-     * <p>
-     * If the argument <code>obj</code> is null, a
-     * <code>NullPointerException</code> is thrown (in the current thread).
-     * <p>
-     * The thread represented by this thread is forced to stop
-     * whatever it is doing abnormally and to throw the
-     * <code>Throwable</code> object <code>obj</code> as an exception. This
-     * is an unusual action to take; normally, the <code>stop</code> method
-     * that takes no arguments should be used.
-     * <p>
-     * It is permitted to stop a thread that has not yet been started.
-     * If the thread is eventually started, it immediately terminates.
-     *
-     * @param      obj   the Throwable object to be thrown.
-     * @exception  SecurityException  if the current thread cannot modify
-     *               this thread.
-     * @throws     NullPointerException if obj is <tt>null</tt>.
-     * @see        #interrupt()
-     * @see        #checkAccess()
-     * @see        #run()
-     * @see        #start()
-     * @see        #stop()
-     * @see        SecurityManager#checkAccess(Thread)
-     * @see        SecurityManager#checkPermission
-     * @deprecated This method is inherently unsafe.  See {@link #stop()}
-     *        for details.  An additional danger of this
-     *        method is that it may be used to generate exceptions that the
-     *        target thread is unprepared to handle (including checked
-     *        exceptions that the thread could not possibly throw, were it
-     *        not for this method).
-     *        For more information, see
-     *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
-     *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
-     */
-    @Deprecated
-    public final synchronized void stop(Throwable obj) {
-        if (obj == null)
-            throw new NullPointerException();
-
         SecurityManager security = System.getSecurityManager();
         if (security != null) {
             checkAccess();
-            if ((this != Thread.currentThread()) ||
-                (!(obj instanceof ThreadDeath))) {
+            if (this != Thread.currentThread()) {
                 security.checkPermission(SecurityConstants.STOP_THREAD_PERMISSION);
             }
         }
@@ -884,7 +826,26 @@
         }
 
         // The VM can handle all thread states
-        stop0(obj);
+        stop0(new ThreadDeath());
+    }
+
+    /**
+     * Throws {@code UnsupportedOperationException}.
+     *
+     * @param obj ignored
+     *
+     * @deprecated This method was originally designed to force a thread to stop
+     *        and throw a given {@code Throwable} as an exception. It was
+     *        inherently unsafe (see {@link #stop()} for details), and furthermore
+     *        could be used to generate exceptions that the target thread was
+     *        not prepared to handle.
+     *        For more information, see
+     *        <a href="{@docRoot}/../technotes/guides/concurrency/threadPrimitiveDeprecation.html">Why
+     *        are Thread.stop, Thread.suspend and Thread.resume Deprecated?</a>.
+     */
+    @Deprecated
+    public final synchronized void stop(Throwable obj) {
+        throw new UnsupportedOperationException();
     }
 
     /**
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/Thread/StopThrowable.java	Tue Jun 11 14:09:06 2013 +0100
@@ -0,0 +1,69 @@
+/*
+ * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * @test
+ * @bug 7059085
+ * @summary Check that Thread.stop(Throwable) throws UOE
+ * @run testng StopThrowable
+ */
+
+import org.testng.annotations.Test;
+
+import java.util.concurrent.CountDownLatch;
+
+public class StopThrowable {
+
+    @Test(expectedExceptions=UnsupportedOperationException.class)
+    public void testStopSelf() {
+        Thread.currentThread().stop(new ThreadDeath());
+    }
+
+    private static void awaitUnchecked(CountDownLatch latch) {
+        try {
+            latch.await();
+        } catch (InterruptedException e) {
+            // should not happen
+            throw new RuntimeException(e);
+        }
+    }
+
+    @Test(expectedExceptions=UnsupportedOperationException.class)
+    public void testStopOther() throws Throwable {
+        CountDownLatch ready = new CountDownLatch(1);
+        CountDownLatch done = new CountDownLatch(1);
+        Thread t = new Thread( () ->  { ready.countDown(); awaitUnchecked(done); } );
+        t.start();
+        try {
+            ready.await();
+            t.stop(new ThreadDeath());
+        } finally {
+            done.countDown();
+        }
+    }
+
+    @Test(expectedExceptions=UnsupportedOperationException.class)
+    public void testNull() {
+        Thread.currentThread().stop(null);
+    }
+}
--- a/jdk/test/java/util/concurrent/Executors/PrivilegedCallables.java	Tue Jun 11 12:13:26 2013 +0200
+++ b/jdk/test/java/util/concurrent/Executors/PrivilegedCallables.java	Tue Jun 11 14:09:06 2013 +0100
@@ -43,7 +43,8 @@
 
     final Random rnd = new Random();
 
-    @SuppressWarnings("serial") Throwable[] throwables = {
+    @SuppressWarnings("serial")
+    Throwable[] throwables = {
         new Exception() {},
         new RuntimeException() {},
         new Error() {}
@@ -51,6 +52,11 @@
     Throwable randomThrowable() {
         return throwables[rnd.nextInt(throwables.length)];
     }
+    void throwThrowable(Throwable t) throws Exception {
+        if (t instanceof Error) throw (Error) t;
+        if (t instanceof RuntimeException) throw (RuntimeException) t;
+        throw (Exception) t;
+    }
 
     //----------------------------------------------------------------
     // A Policy class designed to make permissions fiddling very easy.
@@ -119,9 +125,8 @@
             if (rnd.nextBoolean()) {
                 final Throwable t = randomThrowable();
                 real = new Callable<Integer>() {
-                    @SuppressWarnings("deprecation")
                     public Integer call() throws Exception {
-                        Thread.currentThread().stop(t);
+                        throwThrowable(t);
                         return null; }};
                 try {
                     c.call();
--- a/jdk/test/java/util/concurrent/FutureTask/Throw.java	Tue Jun 11 12:13:26 2013 +0200
+++ b/jdk/test/java/util/concurrent/FutureTask/Throw.java	Tue Jun 11 14:09:06 2013 +0100
@@ -31,10 +31,9 @@
 
 public class Throw {
 
-    @SuppressWarnings("deprecation")
     static void THROW(final Throwable t) {
         if (t != null)
-            Thread.currentThread().stop(t);
+            Throw.<RuntimeException>uncheckedThrow(t);
     }
 
     Callable<Void> thrower(final Throwable t) {
@@ -138,4 +137,8 @@
             catch (Throwable t) {
                 if (k.isAssignableFrom(t.getClass())) pass();
                 else unexpected(t);}}
+    @SuppressWarnings("unchecked") static <T extends Throwable>
+        void uncheckedThrow(Throwable t) throws T {
+        throw (T)t; // rely on vacuous cast
+    }
 }
--- a/jdk/test/java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java	Tue Jun 11 12:13:26 2013 +0200
+++ b/jdk/test/java/util/concurrent/ThreadPoolExecutor/ThrowingTasks.java	Tue Jun 11 14:09:06 2013 +0100
@@ -101,8 +101,10 @@
     static class Thrower implements Runnable {
         Throwable t;
         Thrower(Throwable t) { this.t = t; }
-        @SuppressWarnings("deprecation")
-        public void run() { if (t != null) Thread.currentThread().stop(t); }
+        public void run() {
+            if (t != null)
+                ThrowingTasks.<RuntimeException>uncheckedThrow(t);
+        }
     }
 
     static final Thrower noThrower      = new Thrower(null);
@@ -265,4 +267,8 @@
         try {realMain(args);} catch (Throwable t) {unexpected(t);}
         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
         if (failed > 0) throw new AssertionError("Some tests failed");}
+    @SuppressWarnings("unchecked") static <T extends Throwable>
+        void uncheckedThrow(Throwable t) throws T {
+        throw (T)t; // rely on vacuous cast
+    }
 }
--- a/jdk/test/java/util/concurrent/locks/Lock/FlakyMutex.java	Tue Jun 11 12:13:26 2013 +0200
+++ b/jdk/test/java/util/concurrent/locks/Lock/FlakyMutex.java	Tue Jun 11 14:09:06 2013 +0100
@@ -37,7 +37,7 @@
  * tryAcquire method that randomly throws various Throwable
  * subclasses.
  */
-@SuppressWarnings({"deprecation", "serial"})
+@SuppressWarnings("serial")
 public class FlakyMutex implements Lock {
     static class MyError extends Error {}
     static class MyException extends Exception {}
@@ -49,7 +49,7 @@
         switch (rnd.nextInt(10)) {
         case 0: throw new MyError();
         case 1: throw new MyRuntimeException();
-        case 2: Thread.currentThread().stop(new MyException()); break;
+        case 2: FlakyMutex.<RuntimeException>uncheckedThrow(new MyException());
         default: /* Do nothing */ break;
         }
     }
@@ -146,4 +146,8 @@
         try {realMain(args);} catch (Throwable t) {unexpected(t);}
         System.out.printf("%nPassed = %d, failed = %d%n%n", passed, failed);
         if (failed > 0) throw new AssertionError("Some tests failed");}
+    @SuppressWarnings("unchecked") static <T extends Throwable>
+        void uncheckedThrow(Throwable t) throws T {
+        throw (T)t; // rely on vacuous cast
+    }
 }