Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/interrupt/interrupt001/interrupt001a.java
40955 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.interrupt.interrupt001;2425import nsk.share.*;26import nsk.share.jdb.*;2728import java.io.*;29import java.util.concurrent.Semaphore;30import java.util.concurrent.atomic.AtomicInteger;3132/* This is debuggee aplication */33public class interrupt001a {34private class MyThread extends Thread {35final Object lock;36int ind;37String name;3839public MyThread (Object l, int i, String n) {40lock = l;41ind = i;42name = n;43}4445public void run() {46synchronized (lock) {47synchronized (waitnotify) {48threadRunning = true;49waitnotify.notify();50}5152try {53flags[ind] = false;54while (!flags[ind]) {55lock.wait();56}57} catch (InterruptedException e) {58notInterrupted.decrementAndGet();59synchronized (waitnotify) {60waitnotify.notify();61}62}63}64}65}6667public static void main(String args[]) {68interrupt001a _interrupt001a = new interrupt001a();69System.exit(interrupt001.JCK_STATUS_BASE + _interrupt001a.runIt(args, System.out));70}7172static void breakHere () {}7374static final String MYTHREAD = "MyThread";75static final int numThreads = 5; // number of threads76static volatile boolean allWorkersAreWaiting = false;7778private final Object waitnotify = new Object();79private volatile boolean threadRunning;80private volatile boolean[] flags = new boolean[numThreads];8182private JdbArgumentHandler argumentHandler;83private Log log;8485public static final AtomicInteger notInterrupted = new AtomicInteger(numThreads);8687public int runIt(String args[], PrintStream out) {8889argumentHandler = new JdbArgumentHandler(args);90log = new Log(out, argumentHandler);9192int i;93Thread[] holder = new Thread[numThreads];94Object[] locks = new Object[numThreads];9596for (i = 0; i < numThreads ; i++) {97locks[i] = new Object();98holder[i] = new MyThread(locks[i], i, MYTHREAD + "-" + i);99}100101synchronized (waitnotify) {102for (i = 0; i < numThreads ; i++) {103holder[i].start();104try {105threadRunning = false;106while (!threadRunning) {107waitnotify.wait();108}109} catch (InterruptedException e) {110log.complain("Main thread was interrupted while waiting for start of " + MYTHREAD + "-" + i);111return interrupt001.FAILED;112}113}114}115116// allWorkersAreWaiting will be set to true by the debugger thread117// when it sees all of the worker treads are waiting.118do {119breakHere(); // a break to get thread ids and then to interrupt MyThreads.120} while (!allWorkersAreWaiting);121122long waitTime = argumentHandler.getWaitTime() * 60 * 1000;123long startTime = System.currentTimeMillis();124while (notInterrupted.get() > 0 && System.currentTimeMillis() - startTime <= waitTime) {125synchronized (waitnotify) {126try {127waitnotify.wait(waitTime);128} catch (InterruptedException e) {129log.display("Main thread was interrupted while waiting");130}131}132}133for (i = 0; i < numThreads ; i++) {134if (holder[i].isAlive()) {135synchronized (locks[i]) {136flags[i] = true;137locks[i].notifyAll();138}139}140}141breakHere(); // a break to check if MyThreads were interrupted142143log.display("Debuggee PASSED");144return interrupt001.PASSED;145}146}147148149