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.3 $
022: */
023:
024: /**
025: * Created on 07.06.2006
026: */package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
027:
028: import org.apache.harmony.jpda.tests.share.SyncDebuggee;
029:
030: public class SuspendCountDebuggee extends SyncDebuggee {
031: public static final int THREAD_NUMBER_LIMIT = 6;
032: public static final String THREAD_NAME_PATTERN = "SuspendCountDebuggee_Thread_";
033: public static final String TO_FINISH_DEBUGGEE_FIELD_NAME = "debuggeToFinish";
034:
035: static SuspendCountDebuggee suspendCountDebuggeeThis;
036:
037: static volatile boolean allThreadsToFinish = false;
038: static int debuggeToFinish = 0;
039: static int createdThreadsNumber = 0;
040: static volatile int startedThreadsNumber = 0;
041:
042: static SuspendCountDebuggee_Thread[] suspendCountDebuggeeThreads = null;
043:
044: static Object waitTimeObject = new Object();
045:
046: static void waitMlsecsTime(long mlsecsTime) {
047: synchronized (waitTimeObject) {
048: try {
049: waitTimeObject.wait(mlsecsTime);
050: } catch (Throwable throwable) {
051: // ignore
052: }
053: }
054: }
055:
056: static void sleepMlsecsTime(long mlsecsTime) {
057: try {
058: Thread.sleep(mlsecsTime);
059: } catch (Throwable throwable) {
060: // ignore
061: }
062: }
063:
064: public void run() {
065:
066: logWriter.println("--> SuspendCountDebuggee: START...");
067: suspendCountDebuggeeThis = this ;
068:
069: logWriter
070: .println("--> SuspendCountDebuggee: Create and start tested threads...");
071: try {
072: suspendCountDebuggeeThreads = new SuspendCountDebuggee_Thread[THREAD_NUMBER_LIMIT];
073: for (int i = 0; i < THREAD_NUMBER_LIMIT; i++) {
074: suspendCountDebuggeeThreads[i] = new SuspendCountDebuggee_Thread(
075: i);
076: suspendCountDebuggeeThreads[i].start();
077: createdThreadsNumber++;
078: }
079: } catch (Throwable thrown) {
080: logWriter
081: .println("--> SuspendCountDebuggee: Exception while creating threads: "
082: + thrown);
083: }
084: logWriter
085: .println("--> SuspendCountDebuggee: Created threads number = "
086: + createdThreadsNumber);
087:
088: while (startedThreadsNumber != createdThreadsNumber) {
089: waitMlsecsTime(100);
090: }
091: if (createdThreadsNumber != 0) {
092: logWriter
093: .println("--> SuspendCountDebuggee: All created threads are started!");
094: }
095:
096: synchronizer
097: .sendMessage(Integer.toString(createdThreadsNumber));
098: if (createdThreadsNumber == 0) {
099: logWriter.println("--> SuspendCountDebuggee: FINISH...");
100: System.exit(0);
101: }
102: String messageFromTest = synchronizer.receiveMessage(); // signal to continue or to finish
103:
104: if (!messageFromTest.equals("FINISH")) {
105: String mainThreadName = Thread.currentThread().getName();
106: synchronizer.sendMessage(mainThreadName);
107: while (debuggeToFinish != 99) { // is set up by debugger - SuspendCountTest
108: waitMlsecsTime(100);
109: }
110: }
111:
112: logWriter
113: .println("--> SuspendCountDebuggee: Send signal to all threads to finish and wait...");
114: allThreadsToFinish = true;
115:
116: for (int i = 0; i < createdThreadsNumber; i++) {
117: while (suspendCountDebuggeeThreads[i].isAlive()) {
118: waitMlsecsTime(10);
119: }
120: }
121: logWriter
122: .println("--> SuspendCountDebuggee: All threads finished!");
123:
124: logWriter.println("--> SuspendCountDebuggee: FINISH...");
125:
126: }
127:
128: public static void main(String[] args) {
129: runDebuggee(SuspendCountDebuggee.class);
130: }
131:
132: }
133:
134: class SuspendCountDebuggee_Thread extends Thread {
135:
136: int threadKind;
137:
138: public SuspendCountDebuggee_Thread(int threadNumber) {
139: super (SuspendCountDebuggee.THREAD_NAME_PATTERN + threadNumber);
140: threadKind = threadNumber % 3;
141: }
142:
143: public void run() {
144: SuspendCountDebuggee parent = SuspendCountDebuggee.suspendCountDebuggeeThis;
145: synchronized (parent) {
146: SuspendCountDebuggee.startedThreadsNumber++;
147: }
148: while (!SuspendCountDebuggee.allThreadsToFinish) {
149: switch (threadKind) {
150: case 0:
151: SuspendCountDebuggee.waitMlsecsTime(100);
152: break;
153: case 1:
154: SuspendCountDebuggee.sleepMlsecsTime(100);
155: }
156: }
157: }
158: }
|