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 06.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 SuspendDebuggee extends SyncDebuggee {
031: public static final int THREAD_NUMBER_LIMIT = 9;
032: public static final String THREAD_NAME_PATTERN = "SuspendDebuggee_Thread_";
033:
034: static SuspendDebuggee suspendDebuggeeThis;
035:
036: static volatile boolean allThreadsToFinish = false;
037: static int createdThreadsNumber = 0;
038: static volatile int startedThreadsNumber = 0;
039:
040: static SuspendDebuggee_Thread[] suspendDebuggeeThreads = null;
041:
042: static Object waitTimeObject = new Object();
043:
044: static void waitMlsecsTime(long mlsecsTime) {
045: synchronized (waitTimeObject) {
046: try {
047: waitTimeObject.wait(mlsecsTime);
048: } catch (Throwable throwable) {
049: // ignore
050: }
051: }
052: }
053:
054: static void sleepMlsecsTime(long mlsecsTime) {
055: try {
056: Thread.sleep(mlsecsTime);
057: } catch (Throwable throwable) {
058: // ignore
059: }
060: }
061:
062: public void run() {
063:
064: logWriter.println("--> SuspendDebuggee: START...");
065: suspendDebuggeeThis = this ;
066:
067: logWriter
068: .println("--> SuspendDebuggee: Create and start tested threads...");
069: try {
070: suspendDebuggeeThreads = new SuspendDebuggee_Thread[THREAD_NUMBER_LIMIT];
071: for (int i = 0; i < THREAD_NUMBER_LIMIT; i++) {
072: suspendDebuggeeThreads[i] = new SuspendDebuggee_Thread(
073: i);
074: suspendDebuggeeThreads[i].start();
075: createdThreadsNumber++;
076: }
077: } catch (Throwable thrown) {
078: logWriter
079: .println("--> SuspendDebuggee: Exception while creating threads: "
080: + thrown);
081: }
082: logWriter
083: .println("--> SuspendDebuggee: Created threads number = "
084: + createdThreadsNumber);
085:
086: while (startedThreadsNumber != createdThreadsNumber) {
087: waitMlsecsTime(100);
088: }
089: if (createdThreadsNumber != 0) {
090: logWriter
091: .println("--> SuspendDebuggee: All created threads are started!");
092: }
093:
094: synchronizer
095: .sendMessage(Integer.toString(createdThreadsNumber));
096: if (createdThreadsNumber == 0) {
097: logWriter.println("--> SuspendDebuggee: FINISH...");
098: return;
099: }
100: synchronizer.receiveMessage(); // signal to finish
101:
102: logWriter
103: .println("--> SuspendDebuggee: Send signal to all threads to finish and wait...");
104: allThreadsToFinish = true;
105:
106: for (int i = 0; i < createdThreadsNumber; i++) {
107: while (suspendDebuggeeThreads[i].isAlive()) {
108: waitMlsecsTime(10);
109: }
110: }
111: logWriter.println("--> SuspendDebuggee: All threads finished!");
112:
113: logWriter.println("--> SuspendDebuggee: FINISH...");
114:
115: }
116:
117: public static void main(String[] args) {
118: runDebuggee(SuspendDebuggee.class);
119: }
120:
121: }
122:
123: class SuspendDebuggee_Thread extends Thread {
124:
125: int threadKind;
126:
127: public SuspendDebuggee_Thread(int threadNumber) {
128: super (SuspendDebuggee.THREAD_NAME_PATTERN + threadNumber);
129: threadKind = threadNumber % 3;
130: }
131:
132: public void run() {
133: SuspendDebuggee parent = SuspendDebuggee.suspendDebuggeeThis;
134: synchronized (parent) {
135: SuspendDebuggee.startedThreadsNumber++;
136: }
137: while (!SuspendDebuggee.allThreadsToFinish) {
138: switch (threadKind) {
139: case 0:
140: SuspendDebuggee.waitMlsecsTime(100);
141: break;
142: case 1:
143: SuspendDebuggee.sleepMlsecsTime(100);
144: }
145: }
146: }
147: }
|