Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdb/suspend/suspend001/suspend001a.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.suspend.suspend001;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdb.*;2829import java.io.*;3031/* This is debuggee aplication */32public class suspend001a {33static suspend001a _suspend001a = new suspend001a();3435static JdbArgumentHandler argumentHandler;36static Log log;3738public static void main(String args[]) {39System.exit(suspend001.JCK_STATUS_BASE + _suspend001a.runIt(args, System.out));40}4142static void breakHere () {}4344static Object lock = new Object();45static Object waitnotify = new Object();46public static volatile int notSuspended = 0;4748public int runIt(String args[], PrintStream out) {49argumentHandler = new JdbArgumentHandler(args);50log = new Log(out, argumentHandler);5152Thread suspended = new Suspended("Suspended");53Thread myThread = new MyThread("MyThread");5455// lock monitor to prevent threads from finishing after they started56synchronized (lock) {57synchronized (waitnotify) {58suspended.start();59try {60waitnotify.wait();61} catch (InterruptedException e) {62log.complain("Main thread was interrupted while waiting for start of Suspended thread");63return suspend001.FAILED;64}6566myThread.start();67try {68waitnotify.wait();69} catch (InterruptedException e) {70log.complain("Main thread was interrupted while waiting for start of MyThread thread");71return suspend001.FAILED;72}73}74breakHere(); // a break to get thread ids and then to suspend Suspended thread.75}7677// wait for MyThread completion78if (myThread.isAlive()) {79try {80myThread.join();81} catch (InterruptedException e) {82log.display("Main thread was interrupted while waiting for finish of MyThread thread");83}84}8586// give 3 seconds for suspended thread to finish (if it is not really suspended).87try {88Thread.currentThread().sleep(3 * 1000);89} catch (InterruptedException e) {90log.display("Main thread was interrupted while sleeping");91}9293breakHere(); // a break to check if Suspended was suspended9495log.display("notSuspended == " + notSuspended);96log.display("Debuggee PASSED");97return suspend001.PASSED;98}99100public static int getResult() {101return notSuspended;102}103}104105class Suspended extends Thread {106String name;107108public Suspended (String n) {109name = n;110}111112public void run() {113// Concatenate strings in advance to avoid lambda calculations later114final String ThreadFinished = "Thread finished: " + this.name;115suspend001a.log.display("Thread started: " + this.name);116117synchronized (suspend001a.waitnotify) {118suspend001a.waitnotify.notify();119}120// prevent thread from early finish121synchronized (suspend001a.lock) {}122123suspend001a.notSuspended++;124125suspend001a.log.display(ThreadFinished);126}127}128129class MyThread extends Thread {130String name;131132public MyThread (String n) {133name = n;134}135136public void run() {137// Concatenate strings in advance to avoid lambda calculations later138final String ThreadFinished = "Thread finished: " + this.name;139suspend001a.log.display("Thread started: " + this.name);140141synchronized (suspend001a.waitnotify) {142suspend001a.waitnotify.notify();143}144// prevent thread from early finish145synchronized (suspend001a.lock) {}146147suspend001a.notSuspended++;148149suspend001a.log.display(ThreadFinished);150}151}152153154