Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/trace/trace001/trace001a.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.trace.trace001;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdb.*;2829import java.io.*;3031/* This is debuggee aplication */32public class trace001a {33public static void main(String args[]) {34trace001a _trace001a = new trace001a();35System.exit(trace001.JCK_STATUS_BASE + _trace001a.runIt(args, System.out));36}3738static void lastBreak () {}3940static final String MYTHREAD = "MyThread";41static final int numThreads = 2; // number of threads.4243static Object waitnotify = new Object();4445public int runIt(String args[], PrintStream out) {4647JdbArgumentHandler argumentHandler = new JdbArgumentHandler(args);48Log log = new Log(out, argumentHandler);4950int i;51Thread holder [] = new Thread[numThreads];52Object[] locks = new Object[numThreads];5354for (i = 0; i < numThreads ; i++) {55locks[i] = new Object();56holder[i] = new MyThread(locks[i],MYTHREAD + "-" + i);57}5859synchronized (waitnotify) {60for (i = 0; i < numThreads ; i++) {61holder[i].start();62try {63waitnotify.wait();64} catch (InterruptedException e) {65System.out.println("Main thread was interrupted while waiting for start of " + MYTHREAD + "-" + i);66return trace001.FAILED;67}6869synchronized (locks[i]) { // holder[i] must wait on its lock[i] at this moment.70System.out.println("Thread " + MYTHREAD + "-" + i + " is waiting");71}72}73}74lastBreak(); // a break to get thread ids and then to turn on tracing.7576// waits on all MyThreads completion77for (i = 0; i < numThreads ; i++) {78synchronized (locks[i]) {79locks[i].notifyAll();80}81if (holder[i].isAlive() && !holder[i].interrupted()) {82try {83holder[i].join();84} catch (InterruptedException e) {85System.out.println("Main thread was interrupted while waiting for finish of " + MYTHREAD + "-" + i);86return trace001.FAILED;87}88}89}9091log.display("Debuggee PASSED");92return trace001.PASSED;93}94}959697class MyThread extends Thread {98Object lock;99String name;100101public MyThread (Object l, String n) {102lock = l;103name = n;104}105106public void run() {107// Concatenate strings in advance to avoid lambda calculations later108final String ThreadFinished = "Thread finished: " + this.name;109final String ThreadInterrupted = "Thread was interrupted: " + this.name;110System.out.println("Thread started: " + this.name);111112synchronized (lock) {113synchronized (trace001a.waitnotify) {114trace001a.waitnotify.notify();115}116117try {118lock.wait();119int square = func1(100);120System.out.println(ThreadFinished);121} catch (InterruptedException e) {122System.out.println(ThreadInterrupted);123e.printStackTrace();124}125}126127System.out.println(ThreadFinished);128}129130public int func1(int i) {131return func2(i);132}133134public int func2(int i) {135return func3(i);136}137138public int func3(int i) {139return i*i;140}141}142143144