Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/kill/kill002/kill002a.java
40951 views
/*1* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/22package nsk.jdb.kill.kill002;2324import nsk.share.*;25import nsk.share.jpda.*;26import nsk.share.jdb.*;2728import java.io.*;2930/* This is debuggee aplication */31public class kill002a {32public static void main(String args[]) {33kill002a _kill002a = new kill002a();34System.exit(kill002.JCK_STATUS_BASE + _kill002a.runIt(args, System.out));35}3637static void breakHere () {}3839static final String MYTHREAD = "MyThread";40static final int numThreads = 5; // number of threads41static Object waitnotify = new Object();42public static volatile int notKilled = 0;43static final String message = "kill002a's Exception";4445static JdbArgumentHandler argumentHandler;46static Log log;4748static final Exception[] exceptions = {49new InterruptedException(message),50new NullPointerException(message),51new SecurityException(message),52new com.sun.jdi.IncompatibleThreadStateException(message),53new MyException(message)54};5556public int runIt(String args[], PrintStream out) {5758argumentHandler = new JdbArgumentHandler(args);59log = new Log(out, argumentHandler);6061int i;62Thread[] holder = new Thread[numThreads];63Object[] locks = new Object[numThreads];6465for (i = 0; i < numThreads ; i++) {66locks[i] = new Object();67holder[i] = new MyThread(locks[i], MYTHREAD + "-" + i);68}6970synchronized (waitnotify) {71for (i = 0; i < numThreads ; i++) {72holder[i].start();73try {74waitnotify.wait();75} catch (InterruptedException e) {76log.complain("Main thread was interrupted while waiting for start of " + MYTHREAD + "-" + i);77return kill002.FAILED;78}7980synchronized (locks[i]) { // holder[i] must wait on its lock[i] at this moment.81log.display("Thread " + MYTHREAD + "-" + i + " is waiting");82}83}84}85breakHere(); // a break to get thread ids and then to kill MyThreads.8687// waits on all MyThreads completion in case they were not killed88for (i = 0; i < numThreads ; i++) {89synchronized (locks[i]) {90locks[i].notifyAll();91}92if (holder[i].isAlive() && !holder[i].interrupted()) {93try {94holder[i].join();95} catch (InterruptedException e) {96log.display("Main thread was interrupted while waiting for finish of " + MYTHREAD + "-" + i);97}98}99}100breakHere(); // a break to check if MyThreads were killed101102log.display("notKilled == " + notKilled);103log.display("Debuggee PASSED");104return kill002.PASSED;105}106107static class MyException extends Exception {108MyException (String message) {109super(message);110}111}112}113114115class MyThread extends Thread {116Object lock;117String name;118119public MyThread (Object l, String n) {120lock = l;121name = n;122}123124public void run() {125// Concatenate strings in advance to avoid lambda calculations later126final String ThreadFinished = "WARNING: Thread finished: " + this.name;127String ThreadInterrupted = "WARNING: Thread was interrupted while waiting for killing: " + this.name;128kill002a.log.display("Thread started: " + this.name);129130synchronized (lock) {131synchronized (kill002a.waitnotify) {132kill002a.waitnotify.notify();133}134135try {136lock.wait();137kill002a.notKilled++;138kill002a.log.display(ThreadFinished);139} catch (Exception e) {140kill002a.log.display(ThreadInterrupted);141e.printStackTrace(kill002a.log.getOutStream());142}143}144}145}146147148