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 Vitaly A. Provodin
021: * @version $Revision: 1.3 $
022: */
023:
024: /**
025: * Created on 18.02.2005
026: */package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
027:
028: import org.apache.harmony.jpda.tests.framework.DebuggeeSynchronizer;
029: import org.apache.harmony.jpda.tests.framework.LogWriter;
030: import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
031: import org.apache.harmony.jpda.tests.share.SyncDebuggee;
032:
033: /**
034: * The class specifies debuggee for <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.ThreadGroupTest</code>.
035: * This debuggee is started as follow:
036: * <ol>
037: * <li>the tested group <code>TESTED_GROUP</code> is created
038: * <li>the tested thread <code>TESTED_THREAD</code> is started so this
039: * thread belongs to that thread group
040: * </ol>
041: * For different goals of tests, the debuggee sends the <code>SGNL_READY</code>
042: * signal to and waits for the <code>SGNL_CONTINUE</code> signal from debugger
043: * in two places:
044: * <ul>
045: * <li>right away when the tested thread has been started
046: * <li>when the tested thread has been finished
047: * </ul>
048: */
049: public class ThreadGroupDebuggee extends SyncDebuggee {
050:
051: public static final String TESTED_GROUP = "TestedGroup";
052: public static final String TESTED_THREAD = "TestedThread";
053:
054: static Object waitForStart = new Object();
055: static Object waitForFinish = new Object();
056:
057: static Object waitTimeObject = new Object();
058:
059: static void waitMlsecsTime(long mlsecsTime) {
060: synchronized (waitTimeObject) {
061: try {
062: waitTimeObject.wait(mlsecsTime);
063: } catch (Throwable throwable) {
064: // ignore
065: }
066: }
067: }
068:
069: public void run() {
070: ThreadGroup thrdGroup = new ThreadGroup(TESTED_GROUP);
071: DebuggeeThread thrd = new DebuggeeThread(thrdGroup,
072: TESTED_THREAD, logWriter, synchronizer);
073:
074: synchronized (waitForStart) {
075: thrd.start();
076: try {
077: waitForStart.wait();
078: } catch (InterruptedException e) {
079:
080: }
081: }
082:
083: while (thrd.isAlive()) {
084: waitMlsecsTime(100);
085: }
086:
087: // synchronized(waitForFinish){
088: logWriter.println("thread is finished");
089: // }
090: logWriter.println("send SGNL_READY");
091: synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
092:
093: synchronizer
094: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
095: }
096:
097: class DebuggeeThread extends Thread {
098:
099: LogWriter logWriter;
100: DebuggeeSynchronizer synchronizer;
101:
102: public DebuggeeThread(ThreadGroup thrdGroup, String name,
103: LogWriter logWriter, DebuggeeSynchronizer synchronizer) {
104: super (thrdGroup, name);
105: this .logWriter = logWriter;
106: this .synchronizer = synchronizer;
107: }
108:
109: public void run() {
110:
111: synchronized (ThreadGroupDebuggee.waitForFinish) {
112:
113: synchronized (ThreadGroupDebuggee.waitForStart) {
114:
115: ThreadGroupDebuggee.waitForStart.notifyAll();
116:
117: logWriter.println(getName() + ": started");
118: synchronizer
119: .sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
120:
121: logWriter.println(getName()
122: + ": wait for SGNL_CONTINUE");
123: synchronizer
124: .receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
125: logWriter.println(getName() + ": finished");
126: }
127: }
128: }
129: }
130:
131: public static void main(String[] args) {
132: runDebuggee(ThreadGroupDebuggee.class);
133: }
134: }
|