Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/jdi/LocatableEvent/thread/thread001a.java
41161 views
/*1* Copyright (c) 2001, 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.jdi.LocatableEvent.thread;2425import nsk.share.*;26import nsk.share.jpda.*;27import nsk.share.jdi.*;2829/**30* This class is used as debuggee application for the thread001 JDI test.31*/3233public class thread001a {3435//----------------------------------------------------- templete section3637static final int PASSED = 0;38static final int FAILED = 2;39static final int PASS_BASE = 95;4041static ArgumentHandler argHandler;42static Log log;4344//-------------------------------------------------- log procedures4546private static void log1(String message) {47log.display("**> debuggee: " + message);48}4950private static void logErr(String message) {51log.complain("**> debuggee: " + message);52}5354//====================================================== test program5556static TestClass tcObject = new TestClass();5758static String threadNames[] = {59"awThread", "mwThread", "bpThread", "excThread",60"menThread", "mexThread", "stThread"61};6263static int threadsN = threadNames.length;6465static Thread threads[] = new Thread[threadsN];6667//------------------------------------------------------ common section6869static int exitCode = PASSED;7071static int instruction = 1;72static int end = 0;73// static int quit = 0;74// static int continue = 2;75static int maxInstr = 1; // 2;7677static int lineForComm = 2;7879private static void methodForCommunication() {80int i1 = instruction;81int i2 = i1;82int i3 = i2;83}84//---------------------------------------------------- main method8586public static void main (String argv[]) {8788argHandler = new ArgumentHandler(argv);89log = argHandler.createDebugeeLog();9091log1("debuggee started!");9293label0:94for (int i = 0; ; i++) {9596if (instruction > maxInstr) {97logErr("ERROR: unexpected instruction: " + instruction);98exitCode = FAILED;99break ;100}101102switch (i) {103104//------------------------------------------------------ section tested105106case 0:107for (int n1 = 0; n1 < threadsN; n1++) {108if (n1 < threadsN-1)109threads[n1] = new Thread1thread001a(threadNames[n1]);110else111threads[n1] = new Thread2thread001a(threadNames[n1]);112}113log1(" threads has been created");114115synchronized (lockingObject2) {116log1(" loop: threadStart(threads[n2])");117for (int n2 = 0; n2 < threadsN; n2++)118if ( threadStart(threads[n2]) != PASSED )119break label0;120121log1(" methodForCommunication();");122methodForCommunication();123}124125for (int n2 = 0; n2 < threadsN; n2++) {126synchronized (locks[n2]) {127log1(" synchronized (locks[n2]) : n2 == " + n2);128}129}130131methodForCommunication();132break ;133134//------------------------------------------------- standard end section135136default:137instruction = end;138methodForCommunication();139break label0;140}141}142143System.exit(exitCode + PASS_BASE);144}145146147static Object waitnotifyObj = new Object();148static Object lockingObject = new Object();149150static int threadStart(Thread t) {151synchronized (waitnotifyObj) {152t.start();153try {154// log3("threadStart before: waitnotifyObj.wait();");155waitnotifyObj.wait();156// log3("threadStart after: waitnotifyObj.wait();");157} catch ( Exception e) {158exitCode = FAILED;159logErr(" Exception : " + e );160return FAILED;161}162}163return PASSED;164}165166167public static void nullMethod() {168throw new NullPointerException("test");169}170171static Object lockingObject2 = new Object();172static Object locks[] = new Object[threadsN];173174static volatile int n = 0;175176static class Thread1thread001a extends Thread {177178int threadIndex;179180public Thread1thread001a(String threadName) {181super(threadName);182threadIndex = n;183locks[threadIndex] = new Object();184n++;185}186187public void run() {188log3(" 'run': enter :: threadIndex == " + threadIndex);189190synchronized (locks[threadIndex]) {191log3("enter synchronized (locks[threadIndex]) :: threadIndex == " + threadIndex);192synchronized (waitnotifyObj) {193waitnotifyObj.notify();194}195log3(" 'run': exit synchronized (waitnotifyObj)");196197synchronized (lockingObject2) {198TestClass.method();199}200log3("exit synchronized (locks[threadIndex]) :: threadIndex == " + threadIndex);201}202return;203}204205}206207static class Thread2thread001a extends Thread {208209int threadIndex;210211public Thread2thread001a(String threadName) {212super(threadName);213threadIndex = n;214locks[threadIndex] = new Object();215n++;216}217218public void run() {219log3(" 'run': enter :: threadIndex == " + threadIndex);220221synchronized (locks[threadIndex]) {222log3("enter synchronized (locks[threadIndex]) :: threadIndex == " + threadIndex);223synchronized (waitnotifyObj) {224waitnotifyObj.notify();225}226m1();227log3("exit synchronized (locks[threadIndex]) :: threadIndex == " + threadIndex);228}229return;230}231232private void m1() {233synchronized (lockingObject2) {234log3(" 'm1': enter");235236log3(" 'm1': exit");237}238}239240}241242243public static void log3(String str) {244log1(Thread.currentThread().getName() + " : " + str);245}246247}248249class TestClass {250251static int breakpointLine = 3;252static String awFieldName = "var1";253static String mwFieldName = "var2";254255static int var1 = 0;256static int var2 = 0;257static int var3 = 0;258259static void method () {260var1 += 1;261var3 += 1;262var2 = var3;263try {264thread001a.nullMethod();265} catch ( NullPointerException e ) {266thread001a.log3(" NullPointerException : " + e);267}268}269}270271272