1 /*
  2  * Copyright (c) 2003, 2018, 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 import java.io.PrintStream;
 25 
 26 /*
 27  * @test
 28  *
 29  * @summary converted from VM Testbase nsk/jvmti/ThreadEnd/threadend001.
 30  * VM Testbase keywords: [quick, jpda, jvmti, noras]
 31  * VM Testbase readme:
 32  * DESCRIPTION
 33  *     The test exercises JVMTI event callback function ThreadEnd.
 34  *     The test checks if the event is ganerated by a terminating
 35  *     thread after its initial method has finished execution.
 36  * COMMENTS
 37  *     Ported from JVMDI.
 38  *
 39  * @library /test/lib
 40  * @run main/othervm/native -agentlib:threadend01 threadend01
 41  */
 42 
 43 
 44 public class threadend01 {
 45 
 46     final static int THREADS_LIMIT = 100;
 47     final static String NAME_PREFIX = "threadend01-";
 48 
 49     static {
 50         try {
 51             System.loadLibrary("threadend01");
 52         } catch (UnsatisfiedLinkError ule) {
 53             System.err.println("Could not load threadend01 library");
 54             System.err.println("java.library.path:"
 55                 + System.getProperty("java.library.path"));
 56             throw ule;
 57         }
 58     }
 59 
 60     native static void getReady(int i, String name);
 61     native static int check();
 62 
 63     static volatile int thrCount = THREADS_LIMIT;
 64 
 65     public static void main(String args[]) {
 66         int result = run(args, System.out);
 67         if (result != 0) {
 68             throw new RuntimeException("Unexpected status: " + result);
 69         }
 70     }
 71 
 72     public static int run(String args[], PrintStream out) {
 73         Thread t = new TestThread(NAME_PREFIX + thrCount);
 74         getReady(THREADS_LIMIT, NAME_PREFIX);
 75         t.start();
 76         try {
 77             t.join();
 78         } catch (InterruptedException e) {
 79             throw new Error("Unexpected: " + e);
 80         }
 81         return check();
 82     }
 83 
 84     static class TestThread extends Thread {
 85         public TestThread(String name) {
 86             super(name);
 87         }
 88 
 89         public void run() {
 90             thrCount--;
 91             if (thrCount > 0) {
 92                 TestThread t = new TestThread(NAME_PREFIX + thrCount);
 93                 t.start();
 94                 try {
 95                     t.join();
 96                 } catch (InterruptedException e) {
 97                     throw new Error("Unexpected: " + e);
 98                 }
 99             }
100         }
101     }
102 }