1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  */
18 
19 /**
20  * @author Vitaly A. Provodin
21  */
22 
23 /**
24  * Created on 18.02.2005
25  */
26 package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
27 
28 import org.apache.harmony.jpda.tests.framework.DebuggeeSynchronizer;
29 import org.apache.harmony.jpda.tests.framework.LogWriter;
30 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
31 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
32 
33 
34 /**
35  * The class specifies debuggee for <code>org.apache.harmony.jpda.tests.jdwp.ThreadReference.ThreadGroupTest</code>.
36  * This debuggee is started as follow:
37  * <ol>
38  *      <li>the tested group <code>TESTED_GROUP</code> is created
39  *      <li>the tested thread <code>TESTED_THREAD</code> is started so this
40  *          thread belongs to that thread group
41  * </ol>
42  * For different goals of tests, the debuggee sends the <code>SGNL_READY</code>
43  * signal to and waits for the <code>SGNL_CONTINUE</code> signal from debugger
44  * in two places:
45  * <ul>
46  *      <li>right away when the tested thread has been started
47  *      <li>when the tested thread has been finished
48  * </ul>
49  */
50 public class ThreadGroupDebuggee extends SyncDebuggee {
51 
52     public static final String TESTED_GROUP = "TestedGroup";
53     public static final String TESTED_THREAD = "TestedThread";
54 
55     static Object waitForStart = new Object();
56     static Object waitForFinish = new Object();
57 
58     static Object waitTimeObject = new Object();
waitMlsecsTime(long mlsecsTime)59     static void waitMlsecsTime(long mlsecsTime) {
60         synchronized(waitTimeObject) {
61             try {
62                 waitTimeObject.wait(mlsecsTime);
63             } catch (Throwable throwable) {
64                  // ignore
65             }
66         }
67     }
68 
run()69     public void run() {
70         ThreadGroup thrdGroup = new ThreadGroup(TESTED_GROUP);
71         DebuggeeThread thrd = new DebuggeeThread(thrdGroup, TESTED_THREAD,
72                 logWriter, synchronizer);
73 
74         synchronized(waitForStart){
75             thrd.start();
76             try {
77                 waitForStart.wait();
78             } catch (InterruptedException e) {
79 
80             }
81         }
82 
83         while ( thrd.isAlive() ) {
84             waitMlsecsTime(100);
85         }
86 
87 //        synchronized(waitForFinish){
88             logWriter.println("thread is finished");
89 //        }
90         logWriter.println("send SGNL_READY");
91         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
92 
93         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
94     }
95 
96     class DebuggeeThread extends Thread {
97 
98         LogWriter logWriter;
99         DebuggeeSynchronizer synchronizer;
100 
DebuggeeThread(ThreadGroup thrdGroup, String name, LogWriter logWriter, DebuggeeSynchronizer synchronizer)101         public DebuggeeThread(ThreadGroup thrdGroup, String name,
102                 LogWriter logWriter, DebuggeeSynchronizer synchronizer) {
103             super(thrdGroup, name);
104             this.logWriter = logWriter;
105             this.synchronizer = synchronizer;
106         }
107 
run()108         public void run() {
109 
110             synchronized(ThreadGroupDebuggee.waitForFinish){
111 
112                 synchronized(ThreadGroupDebuggee.waitForStart){
113 
114                     ThreadGroupDebuggee.waitForStart.notifyAll();
115 
116                     logWriter.println(getName() +  ": started");
117                     synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
118 
119                     logWriter.println(getName() +  ": wait for SGNL_CONTINUE");
120                     synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
121                     logWriter.println(getName() +  ": finished");
122                 }
123             }
124         }
125     }
126 
main(String [] args)127     public static void main(String [] args) {
128         runDebuggee(ThreadGroupDebuggee.class);
129     }
130 }
131