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 69 @Override run()70 public void run() { 71 ThreadGroup thrdGroup = new ThreadGroup(TESTED_GROUP); 72 DebuggeeThread thrd = new DebuggeeThread(thrdGroup, TESTED_THREAD, 73 logWriter, synchronizer); 74 75 synchronized(waitForStart){ 76 thrd.start(); 77 try { 78 waitForStart.wait(); 79 } catch (InterruptedException e) { 80 81 } 82 } 83 84 while ( thrd.isAlive() ) { 85 waitMlsecsTime(100); 86 } 87 88 // synchronized(waitForFinish){ 89 logWriter.println("thread is finished"); 90 // } 91 logWriter.println("send SGNL_READY"); 92 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY); 93 94 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 95 } 96 97 static class DebuggeeThread extends Thread { 98 99 LogWriter logWriter; 100 DebuggeeSynchronizer synchronizer; 101 DebuggeeThread(ThreadGroup thrdGroup, String name, LogWriter logWriter, DebuggeeSynchronizer synchronizer)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 @Override run()110 public void run() { 111 112 synchronized(ThreadGroupDebuggee.waitForFinish){ 113 114 synchronized(ThreadGroupDebuggee.waitForStart){ 115 116 ThreadGroupDebuggee.waitForStart.notifyAll(); 117 118 logWriter.println(getName() + ": started"); 119 synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY); 120 121 logWriter.println(getName() + ": wait for SGNL_CONTINUE"); 122 synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE); 123 logWriter.println(getName() + ": finished"); 124 } 125 } 126 } 127 } 128 main(String [] args)129 public static void main(String [] args) { 130 runDebuggee(ThreadGroupDebuggee.class); 131 } 132 } 133