test/java/lang/Thread/StopBeforeStart.java

Print this page




   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4519200
  27  * @summary Confirm a Thread.stop before start complies with the spec
  28  * @author  Pete Soper
  29  *
  30  * Confirm that a thread that had its stop method invoked before start
  31  * does properly terminate with expected exception behavior. NOTE that
  32  * arbitrary application threads could return from their run methods faster
  33  * than the VM can throw an async exception.
  34  */
  35 public class StopBeforeStart {
  36 
  37     private static final int JOIN_TIMEOUT=10000;
  38 
  39     private class MyThrowable extends Throwable {
  40     }
  41 
  42     private class Catcher implements Thread.UncaughtExceptionHandler {
  43         private boolean nullaryStop;
  44         private Throwable theThrowable;
  45         private Throwable expectedThrowable;
  46         private boolean exceptionThrown;
  47 
  48         Catcher(boolean nullaryStop) {
  49             this.nullaryStop = nullaryStop;
  50             if (!nullaryStop) {
  51                 expectedThrowable = new MyThrowable();
  52             }
  53         }
  54 
  55         public void uncaughtException(Thread t, Throwable th) {
  56             exceptionThrown = true;
  57             theThrowable = th;
  58         }
  59 
  60         void check(String label) throws Throwable {
  61             if (!exceptionThrown) {
  62                 throw new RuntimeException(label +
  63                         " test:" + " missing uncaught exception");


  93     public static void main(String args[]) throws Throwable {
  94         (new StopBeforeStart()).doit();
  95         System.out.println("Test passed");
  96     }
  97 
  98     private void doit() throws Throwable {
  99 
 100         runit(false, new Thread(new MyRunnable()),"Thread");
 101         runit(true, new Thread(new MyRunnable()),"Thread");
 102         runit(false, new MyThread(),"Runnable");
 103         runit(true, new MyThread(),"Runnable");
 104     }
 105 
 106     private void runit(boolean nullaryStop, Thread thread,
 107                         String type) throws Throwable {
 108 
 109         Catcher c = new Catcher(nullaryStop);
 110         thread.setUncaughtExceptionHandler(c);
 111 
 112         if (nullaryStop) {
 113             thread.stop();
 114         } else {
 115             thread.stop(c.expectedThrowable);
 116         }
 117 
 118         thread.start();
 119         thread.join(JOIN_TIMEOUT);
 120 
 121         if (thread.getState() != Thread.State.TERMINATED) {
 122 
 123             thread.stop();
 124 
 125             // Under high load this could be a false positive
 126             throw new RuntimeException(type +
 127                         " test:" + " app thread did not terminate");
 128         }
 129 
 130         c.check(type);
 131     }










 132 }


   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug     4519200 6562203
  27  * @summary Confirm a Thread.stop before start complies with the spec
  28  * @author  Pete Soper
  29  *
  30  * Confirm that a thread that had its stop method invoked before start
  31  * does properly terminate with expected exception behavior.


  32  */
  33 public class StopBeforeStart {
  34 
  35     private static final int JOIN_TIMEOUT=10000;
  36 
  37     @SuppressWarnings("serial")
  38     private class MyThrowable extends Throwable { }
  39 
  40     private class Catcher implements Thread.UncaughtExceptionHandler {
  41         private final boolean nullaryStop;
  42         private Throwable theThrowable;
  43         private Throwable expectedThrowable;
  44         private boolean exceptionThrown;
  45 
  46         Catcher(boolean nullaryStop) {
  47             this.nullaryStop = nullaryStop;
  48             if (!nullaryStop) {
  49                 expectedThrowable = new MyThrowable();
  50             }
  51         }
  52 
  53         public void uncaughtException(Thread t, Throwable th) {
  54             exceptionThrown = true;
  55             theThrowable = th;
  56         }
  57 
  58         void check(String label) throws Throwable {
  59             if (!exceptionThrown) {
  60                 throw new RuntimeException(label +
  61                         " test:" + " missing uncaught exception");


  91     public static void main(String args[]) throws Throwable {
  92         (new StopBeforeStart()).doit();
  93         System.out.println("Test passed");
  94     }
  95 
  96     private void doit() throws Throwable {
  97 
  98         runit(false, new Thread(new MyRunnable()),"Thread");
  99         runit(true, new Thread(new MyRunnable()),"Thread");
 100         runit(false, new MyThread(),"Runnable");
 101         runit(true, new MyThread(),"Runnable");
 102     }
 103 
 104     private void runit(boolean nullaryStop, Thread thread,
 105                         String type) throws Throwable {
 106 
 107         Catcher c = new Catcher(nullaryStop);
 108         thread.setUncaughtExceptionHandler(c);
 109 
 110         if (nullaryStop) {
 111             stop(thread);
 112         } else {
 113             stop(thread, c.expectedThrowable);
 114         }
 115 
 116         thread.start();
 117         thread.join(JOIN_TIMEOUT);
 118 
 119         if (thread.getState() != Thread.State.TERMINATED) {
 120 
 121             stop(thread);
 122 
 123             // Under high load this could be a false positive
 124             throw new RuntimeException(type +
 125                         " test:" + " app thread did not terminate");
 126         }
 127 
 128         c.check(type);
 129     }
 130 
 131     @SuppressWarnings("deprecation")
 132     void stop(Thread thread) {
 133         thread.stop();
 134     }
 135 
 136     @SuppressWarnings("deprecation")
 137     void stop(Thread thread, Throwable t) {
 138         thread.stop(t);
 139     }
 140 }