1 /*
2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
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 8167108
27  * @summary Stress test java.lang.Thread.stop() at thread exit.
28  * @run main/othervm -Xlog:thread+smr=debug StopAtExit
29  */
30 
31 import java.util.concurrent.CountDownLatch;
32 import java.util.concurrent.TimeUnit;
33 
34 public class StopAtExit extends Thread {
35     final static int N_THREADS = 32;
36     final static int N_LATE_CALLS = 1000;
37 
38     public CountDownLatch exitSyncObj = new CountDownLatch(1);
39     public CountDownLatch startSyncObj = new CountDownLatch(1);
40 
41     @Override
run()42     public void run() {
43         try {
44             // Tell main thread we have started.
45             startSyncObj.countDown();
46             try {
47                 // Wait for main thread to interrupt us so we
48                 // can race to exit.
49                 exitSyncObj.await();
50             } catch (InterruptedException e) {
51                 // ignore because we expect one
52             }
53         } catch (ThreadDeath td) {
54             // ignore because we're testing Thread.stop() which throws it
55         } catch (NoClassDefFoundError ncdfe) {
56             // ignore because we're testing Thread.stop() which can cause it
57         }
58     }
59 
main(String[] args)60     public static void main(String[] args) {
61         StopAtExit threads[] = new StopAtExit[N_THREADS];
62 
63         for (int i = 0; i < N_THREADS; i++ ) {
64             threads[i] = new StopAtExit();
65             int late_count = 1;
66             threads[i].start();
67             try {
68                 // Wait for the worker thread to get going.
69                 threads[i].startSyncObj.await();
70 
71                 // This interrupt() call will break the worker out
72                 // of the exitSyncObj.await() call and the stop()
73                 // calls will come in during thread exit.
74                 threads[i].interrupt();
75                 for (; late_count <= N_LATE_CALLS; late_count++) {
76                     threads[i].stop();
77 
78                     if (!threads[i].isAlive()) {
79                         // Done with Thread.stop() calls since
80                         // thread is not alive.
81                         break;
82                     }
83                 }
84             } catch (InterruptedException e) {
85                 throw new Error("Unexpected: " + e);
86             } catch (NoClassDefFoundError ncdfe) {
87                 // Ignore because we're testing Thread.stop() which can
88                 // cause it. Yes, a NoClassDefFoundError that happens
89                 // in a worker thread can subsequently be seen in the
90                 // main thread.
91             }
92 
93             System.out.println("INFO: thread #" + i + ": made " + late_count +
94                                " late calls to java.lang.Thread.stop()");
95             System.out.println("INFO: thread #" + i + ": N_LATE_CALLS==" +
96                                N_LATE_CALLS + " value is " +
97                                ((late_count >= N_LATE_CALLS) ? "NOT " : "") +
98                                "large enough to cause a Thread.stop() " +
99                                "call after thread exit.");
100 
101             try {
102                 threads[i].join();
103             } catch (InterruptedException e) {
104                 throw new Error("Unexpected: " + e);
105             }
106             threads[i].stop();
107             if (threads[i].isAlive()) {
108                 throw new Error("Expected !Thread.isAlive() after thread #" +
109                                 i + " has been join()'ed");
110             }
111         }
112 
113         String cmd = System.getProperty("sun.java.command");
114         if (cmd != null && !cmd.startsWith("com.sun.javatest.regtest.agent.MainWrapper")) {
115             // Exit with success in a non-JavaTest environment:
116             System.exit(0);
117         }
118     }
119 }
120