Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/untrace/untrace001/untrace001a.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.untrace.untrace001;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdb.*;2829import java.io.*;3031/* This is debuggee aplication */32public class untrace001a {3334public static void main(String args[]) {35untrace001a _untrace001a = new untrace001a();36System.exit(untrace001.JCK_STATUS_BASE + _untrace001a.runIt(args, System.out));37}3839static void breakHere() {}4041static final String MYTHREAD = "MyThread";42static final int numThreads = 1; // number of threads.4344static JdbArgumentHandler argumentHandler;45static Log log;4647static Object mainThreadLock0 = new Object();48static Object mainThreadLock1 = new Object();49static volatile boolean mainThreadRunning;50static volatile boolean[] flags = new boolean[numThreads];5152public int runIt(String args[], PrintStream out) {5354argumentHandler = new JdbArgumentHandler(args);55log = new Log(out, argumentHandler);5657int i;58Thread holder [] = new Thread[numThreads];59Object[] locks = new Object[numThreads];6061for (i = 0; i < numThreads ; i++) {62locks[i] = new Object();63holder[i] = new MyThread(locks[i], i, MYTHREAD + "-" + i);64}6566synchronized (mainThreadLock0) {67synchronized (mainThreadLock1) {68for (i = 0; i < numThreads ; i++) {69holder[i].start();70try {71mainThreadRunning = false;72while (!mainThreadRunning) {73mainThreadLock1.wait();74}75} catch (InterruptedException e) {76log.complain("Main thread was interrupted while waiting for start of " + MYTHREAD + "-" + i);77return untrace001.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 turn on tracing.8687// waits on all MyThreads completion88for (i = 0; i < numThreads ; i++) {89synchronized (locks[i]) {90flags[i] = true;91locks[i].notifyAll();92}9394try {95mainThreadRunning = false;96while (!mainThreadRunning) {97mainThreadLock0.wait();98}99} catch (InterruptedException e) {100log.complain("Main thread was interrupted while waiting for " + MYTHREAD + "-" + i);101return untrace001.FAILED;102}103104breakHere(); // a break to turn off tracing.105106synchronized (locks[i]) {107flags[i] = true;108locks[i].notifyAll();109}110111if (holder[i].isAlive() && !holder[i].interrupted()) {112try {113holder[i].join();114} catch (InterruptedException e) {115log.complain("Main thread was interrupted while waiting for finish of " + MYTHREAD + "-" + i);116return untrace001.FAILED;117}118}119}120}121122log.display("Debuggee PASSED");123return untrace001.PASSED;124}125}126127128class MyThread extends Thread {129Object lock;130int ind;131String name;132133public MyThread (Object l, int i, String n) {134lock = l;135ind = i;136name = n;137}138139public void run() {140// Concatenate strings in advance to avoid lambda calculations later141final String ThreadFinished = "Thread finished: " + this.name;142final String ThreadInterrupted = "Thread was interrupted: " + this.name;143untrace001a.log.display("Thread started: " + this.name);144145synchronized (lock) {146synchronized (untrace001a.mainThreadLock1) {147untrace001a.mainThreadRunning = true;148untrace001a.mainThreadLock1.notify();149}150151try {152untrace001a.flags[ind] = false;153while (!untrace001a.flags[ind]) {154lock.wait();155}156int square = func1(2);157158synchronized (untrace001a.mainThreadLock0) {159untrace001a.mainThreadRunning = true;160untrace001a.mainThreadLock0.notify();161}162163untrace001a.flags[ind] = false;164while (!untrace001a.flags[ind]) {165lock.wait();166}167square = func1(3);168169untrace001a.log.display(ThreadFinished);170} catch (InterruptedException e) {171untrace001a.log.display(ThreadInterrupted);172}173}174175untrace001a.log.display(ThreadFinished);176}177178public int func1(int i) {179return func2(i);180}181182public int func2(int i) {183return func3(i);184}185186public int func3(int i) {187return i*i;188}189}190191192