001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Anatoly F. Bondarenko
021: * @version $Revision: 1.2 $
022: */
023:
024: /**
025: * Created on 19.06.2006
026: */package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
027:
028: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
029: import org.apache.harmony.jpda.tests.share.SyncDebuggee;
030:
031: public class ThreadGroup002Debuggee extends SyncDebuggee {
032: public static final int THREAD_NUMBER_LIMIT = 6;
033: public static final String THREAD_NAME_PATTERN = "ThreadGroup002Debuggee_Thread_";
034: public static final String THREAD_GROUP_NAME_PATTERN = "ThreadGroup002Debuggee_Thread_Group_";
035:
036: static ThreadGroup002Debuggee ThreadGroup002DebuggeeThis;
037:
038: static volatile boolean allThreadsToFinish = false;
039: static int createdThreadsNumber = 0;
040: static volatile int startedThreadsNumber = 0;
041:
042: static int firstThreadsNumber = 0;
043:
044: static ThreadGroup002Debuggee_Thread[] ThreadGroup002DebuggeeThreads = null;
045: static ThreadGroup[] ThreadGroup002DebuggeeThreadGroups = new ThreadGroup[2];
046:
047: static Object waitTimeObject = new Object();
048:
049: static void waitMlsecsTime(long mlsecsTime) {
050: synchronized (waitTimeObject) {
051: try {
052: waitTimeObject.wait(mlsecsTime);
053: } catch (Throwable throwable) {
054: // ignore
055: }
056: }
057: }
058:
059: static void sleepMlsecsTime(long mlsecsTime) {
060: try {
061: Thread.sleep(mlsecsTime);
062: } catch (Throwable throwable) {
063: // ignore
064: }
065: }
066:
067: public void run() {
068:
069: logWriter.println("--> ThreadGroup002Debuggee: START...");
070: ThreadGroup002DebuggeeThis = this ;
071: logWriter
072: .println("--> ThreadGroup002Debuggee: Create thread groups...");
073: ThreadGroup002DebuggeeThreadGroups[0] = new ThreadGroup(
074: THREAD_GROUP_NAME_PATTERN + 0);
075: ThreadGroup002DebuggeeThreadGroups[1] = new ThreadGroup(
076: THREAD_GROUP_NAME_PATTERN + 1);
077:
078: logWriter
079: .println("--> ThreadGroup002Debuggee: Create and start tested threads...");
080: try {
081: ThreadGroup002DebuggeeThreads = new ThreadGroup002Debuggee_Thread[THREAD_NUMBER_LIMIT];
082: for (int i = 0; i < THREAD_NUMBER_LIMIT; i++) {
083: ThreadGroup002DebuggeeThreads[i] = new ThreadGroup002Debuggee_Thread(
084: ThreadGroup002DebuggeeThreadGroups[i % 2], i);
085: ThreadGroup002DebuggeeThreads[i].start();
086: createdThreadsNumber++;
087: }
088: } catch (Throwable thrown) {
089: logWriter
090: .println("--> ThreadGroup002Debuggee: Exception while creating threads: "
091: + thrown);
092: }
093: logWriter
094: .println("--> ThreadGroup002Debuggee: Created threads number = "
095: + createdThreadsNumber);
096:
097: while (startedThreadsNumber != createdThreadsNumber) {
098: waitMlsecsTime(100);
099: }
100: if (createdThreadsNumber != 0) {
101: logWriter
102: .println("--> ThreadGroup002Debuggee: All created threads are started!");
103: }
104:
105: synchronizer
106: .sendMessage(Integer.toString(createdThreadsNumber));
107:
108: String mainThreadName = Thread.currentThread().getName();
109: synchronizer.sendMessage(mainThreadName);
110:
111: String mainThreadGroupName = Thread.currentThread()
112: .getThreadGroup().getName();
113: synchronizer.sendMessage(mainThreadGroupName);
114:
115: logWriter
116: .println("--> ThreadGroup002Debuggee: Wait for signal from test to continue...");
117: String messageFromTest = synchronizer.receiveMessage(); // signal to continue or to finish
118:
119: if (!messageFromTest.equals("FINISH")) {
120: logWriter
121: .println("--> ThreadGroup002Debuggee: Send signal to the first threads to finish...");
122:
123: firstThreadsNumber = createdThreadsNumber / 2;
124: for (int i = 0; i < firstThreadsNumber; i++) {
125: while (ThreadGroup002DebuggeeThreads[i].isAlive()) {
126: waitMlsecsTime(10);
127: }
128: }
129: logWriter
130: .println("--> ThreadGroup002Debuggee: First threads finished - number of finished threads = "
131: + firstThreadsNumber);
132:
133: synchronizer
134: .sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
135:
136: logWriter
137: .println("--> ThreadGroup002Debuggee: Wait for signal from test to finish...");
138: synchronizer.receiveMessage(); // signal to finish
139: }
140:
141: logWriter
142: .println("--> ThreadGroup002Debuggee: Send signal to all threads to finish and wait...");
143: allThreadsToFinish = true;
144:
145: for (int i = 0; i < createdThreadsNumber; i++) {
146: while (ThreadGroup002DebuggeeThreads[i].isAlive()) {
147: waitMlsecsTime(10);
148: }
149: }
150: logWriter
151: .println("--> ThreadGroup002Debuggee: All threads finished!");
152:
153: logWriter.println("--> ThreadGroup002Debuggee: FINISH...");
154:
155: }
156:
157: public static void main(String[] args) {
158: runDebuggee(ThreadGroup002Debuggee.class);
159: }
160:
161: }
162:
163: class ThreadGroup002Debuggee_Thread extends Thread {
164:
165: int threadNumber;
166: int threadKind;
167:
168: public ThreadGroup002Debuggee_Thread(ThreadGroup group,
169: int threadNumber) {
170: super (group, ThreadGroup002Debuggee.THREAD_NAME_PATTERN
171: + threadNumber);
172: this .threadNumber = threadNumber;
173: threadKind = threadNumber % 3;
174: }
175:
176: public void run() {
177: ThreadGroup002Debuggee parent = ThreadGroup002Debuggee.ThreadGroup002DebuggeeThis;
178: synchronized (parent) {
179: ThreadGroup002Debuggee.startedThreadsNumber++;
180: }
181: while (!ThreadGroup002Debuggee.allThreadsToFinish) {
182: switch (threadKind) {
183: case 0:
184: ThreadGroup002Debuggee.waitMlsecsTime(100);
185: break;
186: case 1:
187: ThreadGroup002Debuggee.sleepMlsecsTime(100);
188: }
189: if (threadNumber < ThreadGroup002Debuggee.firstThreadsNumber) {
190: return;
191: }
192: }
193: }
194: }
|