Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jvmti/GetCurrentContendedMonitor/contmon001.java
40948 views
/*1* Copyright (c) 2003, 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.jvmti.GetCurrentContendedMonitor;2425import java.io.PrintStream;2627public class contmon001 {2829native static void checkMon(int point, Thread thr, Object mon);30native static int getRes();3132static {33try {34System.loadLibrary("contmon001");35} catch (UnsatisfiedLinkError ule) {36System.err.println("Could not load contmon001 library");37System.err.println("java.library.path:"38+ System.getProperty("java.library.path"));39throw ule;40}41}4243public static volatile boolean startingBarrier = true;44public static volatile boolean waitingBarrier = true;45static Object lockFld = new Object();4647static boolean DEBUG_MODE = false;48static PrintStream out;4950public static void main(String[] args) {51args = nsk.share.jvmti.JVMTITest.commonInit(args);5253System.exit(run(args, System.out) + 95/*STATUS_TEMP*/);54}5556public static void doSleep() {57try {58Thread.sleep(10);59} catch (Exception e) {60throw new Error("Unexpected " + e);61}62}6364public static int run(String argv[], PrintStream ref) {65out = ref;66for (int i = 0; i < argv.length; i++) {67if (argv[i].equals("-v")) // verbose mode68DEBUG_MODE = true;69}7071Object lock = new Object();72Thread currThr = Thread.currentThread();7374if (DEBUG_MODE)75out.println("\nCheck #1: verifying a contended monitor of current thread \""76+ currThr.getName() + "\" ...");77synchronized (lock) {78checkMon(1, currThr, null);79}80if (DEBUG_MODE)81out.println("Check #1 done");8283contmon001a thr = new contmon001a();8485thr.start();86if (DEBUG_MODE)87out.println("\nWaiting for auxiliary thread ...");88while (startingBarrier) {89doSleep();90}91if (DEBUG_MODE)92out.println("Auxiliary thread is ready");9394if (DEBUG_MODE)95out.println("\nCheck #3: verifying a contended monitor of auxiliary thread ...");96checkMon(3, thr, null);97if (DEBUG_MODE)98out.println("Check #3 done");99100thr.letItGo();101102while (waitingBarrier) {103doSleep();104}105synchronized (lockFld) {106if (DEBUG_MODE)107out.println("\nMain thread entered lockFld's monitor"108+ "\n\tand calling lockFld.notifyAll() to awake auxiliary thread");109lockFld.notifyAll();110if (DEBUG_MODE)111out.println("\nCheck #4: verifying a contended monitor of auxiliary thread ...");112checkMon(4, thr, lockFld);113if (DEBUG_MODE)114out.println("Check #4 done");115}116117if (DEBUG_MODE)118out.println("\nMain thread released lockFld's monitor"119+ "\n\tand waiting for auxiliary thread death ...");120121try {122thr.join();123} catch (InterruptedException e) {124throw new Error("Unexpected " + e);125}126if (DEBUG_MODE)127out.println("\nCheck #5: verifying a contended monitor of dead auxiliary thread ...");128checkMon(5, thr, null);129if (DEBUG_MODE)130out.println("Check #5 done");131132return getRes();133}134}135136137class contmon001a extends Thread {138private volatile boolean flag = true;139140public void run() {141if (contmon001.DEBUG_MODE)142contmon001.out.println("check #2: verifying a contended monitor of current auxiliary thread ...");143contmon001.checkMon(2, currentThread(), null);144if (contmon001.DEBUG_MODE)145contmon001.out.println("check #2 done");146147if (contmon001.DEBUG_MODE)148contmon001.out.println("notifying main thread");149contmon001.startingBarrier = false;150151if (contmon001.DEBUG_MODE)152contmon001.out.println("thread is going to loop while <flag> is true ...");153int i = 0;154int n = 1000;155while (flag) {156if (n <= 0) {157n = 1000;158}159if (i > n) {160i = 0;161n--;162}163i++;164}165if (contmon001.DEBUG_MODE)166contmon001.out.println("looping is done: <flag> is false");167168synchronized (contmon001.lockFld) {169contmon001.waitingBarrier = false;170if (contmon001.DEBUG_MODE)171contmon001.out.println("\nthread entered lockFld's monitor"172+ "\n\tand releasing it through the lockFld.wait() call");173try {174contmon001.lockFld.wait();175} catch (InterruptedException e) {176throw new Error("Unexpected " + e);177}178}179180if (contmon001.DEBUG_MODE)181contmon001.out.println("thread exiting");182}183184public void letItGo() {185flag = false;186}187}188189190