50220
|
1 |
/*
|
|
2 |
* Copyright (c) 2002, 2018, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
|
22 |
*/
|
|
23 |
|
|
24 |
package nsk.jdb.threadgroup.threadgroup002;
|
|
25 |
|
|
26 |
import nsk.share.*;
|
|
27 |
import nsk.share.jpda.*;
|
|
28 |
import nsk.share.jdb.*;
|
|
29 |
|
|
30 |
import java.io.*;
|
|
31 |
|
|
32 |
/* This is debuggee aplication */
|
|
33 |
public class threadgroup002a {
|
|
34 |
public static void main(String args[]) {
|
|
35 |
threadgroup002a _threadgroup002a = new threadgroup002a();
|
|
36 |
System.exit(threadgroup002.JCK_STATUS_BASE + _threadgroup002a.runIt(args, System.out));
|
|
37 |
}
|
|
38 |
|
|
39 |
static void lastBreak () {}
|
|
40 |
|
|
41 |
static int numThreadGroups = 3;
|
|
42 |
static int numThreads = 15;
|
|
43 |
static Object waitnotify = new Object();
|
|
44 |
final static String THREADGROUP_NAME = "MyThreadGroup#";
|
|
45 |
|
|
46 |
public int runIt(String args[], PrintStream out) {
|
|
47 |
JdbArgumentHandler argumentHandler = new JdbArgumentHandler(args);
|
|
48 |
Log log = new Log(out, argumentHandler);
|
|
49 |
|
|
50 |
ThreadGroup tgHolder[] = new ThreadGroup[numThreadGroups];
|
|
51 |
Thread holder [] = new Thread[numThreads];
|
|
52 |
Lock lock = new Lock();
|
|
53 |
|
|
54 |
for (int i = 0; i < numThreadGroups ; i++ )
|
|
55 |
tgHolder[i] = new ThreadGroup(THREADGROUP_NAME + i);
|
|
56 |
|
|
57 |
try {
|
|
58 |
lock.setLock();
|
|
59 |
int factor = numThreads / numThreadGroups;
|
|
60 |
int k;
|
|
61 |
for (int i = 0; i < numThreadGroups ; i++) {
|
|
62 |
for (int j = 0; j < factor ; j++) {
|
|
63 |
k = i * factor + j;
|
|
64 |
holder[k] = new MyThread(lock, tgHolder[i], "MyThread#");
|
|
65 |
synchronized (waitnotify) {
|
|
66 |
holder[k].start();
|
|
67 |
waitnotify.wait();
|
|
68 |
}
|
|
69 |
}
|
|
70 |
}
|
|
71 |
} catch (Exception e) {
|
|
72 |
System.err.println("TEST ERROR: Caught unexpected Exception while waiting in main thread: " +
|
|
73 |
e.getMessage());
|
|
74 |
System.exit(threadgroup002.FAILED);
|
|
75 |
}
|
|
76 |
|
|
77 |
lastBreak(); // When jdb stops here, there should be 5 running MyThreads.
|
|
78 |
lock.releaseLock();
|
|
79 |
|
|
80 |
for (int i = 0; i < numThreads ; i++) {
|
|
81 |
if (holder[i].isAlive()) {
|
|
82 |
try {
|
|
83 |
holder[i].join(argumentHandler.getWaitTime() * 60000);
|
|
84 |
} catch (InterruptedException e) {
|
|
85 |
throw new Failure("Unexpected InterruptedException catched while waiting for join of: " + holder[i]);
|
|
86 |
}
|
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
log.display("Debuggee PASSED");
|
|
91 |
return threadgroup002.PASSED;
|
|
92 |
}
|
|
93 |
|
|
94 |
}
|
|
95 |
|
|
96 |
class Lock {
|
|
97 |
boolean lockSet;
|
|
98 |
|
|
99 |
synchronized void setLock() throws InterruptedException {
|
|
100 |
while (lockSet == true)
|
|
101 |
wait();
|
|
102 |
lockSet = true;
|
|
103 |
}
|
|
104 |
|
|
105 |
synchronized void releaseLock() {
|
|
106 |
if (lockSet == true) {
|
|
107 |
lockSet = false;
|
|
108 |
notify();
|
|
109 |
}
|
|
110 |
}
|
|
111 |
}
|
|
112 |
|
|
113 |
class MyThread extends Thread {
|
|
114 |
|
|
115 |
Lock lock;
|
|
116 |
MyThread (Lock l, ThreadGroup group, String name) {
|
|
117 |
super(group, name);
|
|
118 |
this.lock = l;
|
|
119 |
}
|
|
120 |
|
|
121 |
public void run() {
|
|
122 |
synchronized (threadgroup002a.waitnotify) {
|
|
123 |
threadgroup002a.waitnotify.notifyAll();
|
|
124 |
}
|
|
125 |
try {
|
|
126 |
lock.setLock();
|
|
127 |
} catch(Exception e) {
|
|
128 |
System.err.println("TEST ERROR: Caught unexpected Exception while waiting in MyThread: " +
|
|
129 |
e.getMessage());
|
|
130 |
System.exit(threadgroup002.FAILED);
|
|
131 |
}
|
|
132 |
lock.releaseLock();
|
|
133 |
}
|
|
134 |
}
|