Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/kill/kill001/kill001a.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*/2223package nsk.jdb.kill.kill001;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdb.*;2829import java.io.*;30import java.util.*;3132/* This is debuggee aplication */33public class kill001a {34public static void main(String args[]) {35kill001a _kill001a = new kill001a();36System.exit(kill001.JCK_STATUS_BASE + _kill001a.runIt(args, System.out));37}3839static void breakHere () {}4041static final String MYTHREAD = "MyThread";42static final int numThreads = 5; // number of threads. one lock per thread.43static Object lock = new Object();44static Object waitnotify = new Object();45public static volatile int notKilled = 0;46static final String message = "kill001a's Exception";47static int waitTime;4849static JdbArgumentHandler argumentHandler;50static Log log;5152static final Throwable[] exceptions = {53new ThreadDeath(),54new NullPointerException(message),55new SecurityException(message),56new com.sun.jdi.IncompatibleThreadStateException(message),57new MyException(message)58};596061public int runIt(String args[], PrintStream out) {6263argumentHandler = new JdbArgumentHandler(args);64log = new Log(out, argumentHandler);65waitTime = argumentHandler.getWaitTime() * 60 * 1000;6667int i;68Thread holder [] = new Thread[numThreads];6970for (i = 0; i < numThreads ; i++) {71holder[i] = new MyThread(MYTHREAD + "-" + i);72}7374// lock monitor to prevent threads from finishing after they started75synchronized (lock) {76synchronized (waitnotify) {77for (i = 0; i < numThreads ; i++) {78holder[i].start();79try {80waitnotify.wait();81} catch (InterruptedException e) {82log.complain("Main thread was interrupted while waiting for start of " + MYTHREAD + "-" + i);83return kill001.FAILED;84}85}86}87breakHere(); // a break to get thread ids and then to kill MyThreads.88}8990// wait during watTime until all MyThreads will be killed91long oldTime = System.currentTimeMillis();92while ((System.currentTimeMillis() - oldTime) <= kill001a.waitTime) {93boolean waited = false;94for (i = 0; i < numThreads ; i++) {95if (holder[i].isAlive()) {96waited = true;97try {98synchronized(waitnotify) {99waitnotify.wait(1000);100}101} catch (InterruptedException e) {102log.display("Main thread was interrupted while waiting for killing of " + MYTHREAD + "-" + i);103}104}105}106if (!waited) {107break;108}109}110breakHere(); // a break to check if MyThreads were killed111log.display("notKilled == " + notKilled);112113for (i = 0; i < numThreads ; i++) {114if (holder[i].isAlive()) {115log.display("Debuggee FAILED");116return kill001.FAILED;117}118}119120log.display("Debuggee PASSED");121return kill001.PASSED;122}123124static class MyException extends Exception {125MyException (String message) {126super(message);127}128}129}130131132class MyThread extends Thread {133String name;134135public MyThread (String n) {136name = n;137}138139public void run() {140// Concatenate strings in advance to avoid lambda calculations later141String ThreadFinished = "WARNING: Thread finished: " + this.name;142String ThreadInterrupted = "WARNING: Thread was interrupted while waiting for killing: " + this.name;143kill001a.log.display("Thread started: " + this.name);144145synchronized (kill001a.waitnotify) {146kill001a.waitnotify.notify();147}148// prevent thread from early finish149synchronized (kill001a.lock) {}150151// sleep during waitTime to give debugger a chance to kill debugee's thread152try {153Thread.currentThread().sleep(kill001a.waitTime);154} catch (InterruptedException e) {155kill001a.log.display(ThreadInterrupted);156e.printStackTrace(kill001a.log.getOutStream());157}158159kill001a.notKilled++;160kill001a.log.display(ThreadFinished);161}162}163164165