Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/threadgroups/threadgroups002/threadgroups002a.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.threadgroups.threadgroups002;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdb.*;2829import java.io.*;3031/* This is debuggee aplication */32public class threadgroups002a {33public static void main(String args[]) {34threadgroups002a _threadgroups002a = new threadgroups002a();35System.exit(threadgroups002.JCK_STATUS_BASE + _threadgroups002a.runIt(args, System.out));36}3738static void lastBreak () {}3940static int numThreadGroups = 3;41static int numThreads = 15;42static Object waitnotify = new Object();43final static String THREADGROUP_NAME = "MyThreadGroup#";4445public int runIt(String args[], PrintStream out) {46JdbArgumentHandler argumentHandler = new JdbArgumentHandler(args);47Log log = new Log(out, argumentHandler);4849ThreadGroup tgHolder[] = new ThreadGroup[numThreadGroups];50Thread holder [] = new Thread[numThreads];51Lock lock = new Lock();5253for (int i = 0; i < numThreadGroups ; i++ )54tgHolder[i] = new ThreadGroup(THREADGROUP_NAME + i);5556try {57lock.setLock();58int factor = numThreads / numThreadGroups;59int k;60for (int i = 0; i < numThreadGroups ; i++) {61for (int j = 0; j < factor ; j++) {62k = i * factor + j;63holder[k] = new MyThread(lock, tgHolder[i], "MyThread#");64synchronized (waitnotify) {65holder[k].start();66waitnotify.wait();67}68}69}70} catch (Exception e) {71System.err.println("TEST ERROR: Caught unexpected Exception while waiting in main thread: " +72e.getMessage());73System.exit(threadgroups002.FAILED);74}7576lastBreak(); // When jdb stops here, there should be 5 running MyThreads.77lock.releaseLock();7879for (int i = 0; i < numThreads ; i++) {80if (holder[i].isAlive()) {81try {82holder[i].join(argumentHandler.getWaitTime() * 60000);83} catch (InterruptedException e) {84throw new Failure("Unexpected InterruptedException catched while waiting for join of: " + holder[i]);85}86}87}8889log.display("Debuggee PASSED");90return threadgroups002.PASSED;91}9293}9495class Lock {96boolean lockSet;9798synchronized void setLock() throws InterruptedException {99while (lockSet == true)100wait();101lockSet = true;102}103104synchronized void releaseLock() {105if (lockSet == true) {106lockSet = false;107notify();108}109}110}111112class MyThread extends Thread {113114Lock lock;115MyThread (Lock l, ThreadGroup group, String name) {116super(group, name);117this.lock = l;118}119120public void run() {121synchronized (threadgroups002a.waitnotify) {122threadgroups002a.waitnotify.notifyAll();123}124try {125lock.setLock();126} catch(Exception e) {127System.err.println("TEST ERROR: Caught unexpected Exception while waiting in MyThread: " +128e.getMessage());129System.exit(threadgroups002.FAILED);130}131lock.releaseLock();132}133}134135136