Home | History | Annotate | Download | only in VirtualMachine
      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 19.02.2005
     25  */
     26 package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
     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.VirtualMachine.AllThreadsTest</code>.
     36  * This debuggee starts the tested thread <code>TESTED_THREAD</code> and for
     37  * different goals of tests, the debuggee sends the <code>SGNL_READY</code>
     38  * signal to and waits for the <code>SGNL_CONTINUE</code> signal from debugger
     39  * in two places:
     40  * <ul>
     41  *      <li>right away when the tested thread has been started
     42  *      <li>when the tested thread has been finished
     43  * </ul>
     44  */
     45 public class AllThreadsDebuggee extends SyncDebuggee {
     46 
     47     static Object waitTimeObject = new Object();
     48     static void waitMlsecsTime(long mlsecsTime) {
     49         synchronized(waitTimeObject) {
     50             try {
     51                 waitTimeObject.wait(mlsecsTime);
     52             } catch (Throwable throwable) {
     53                  // ignore
     54             }
     55         }
     56     }
     57 
     58     public static final String TESTED_THREAD = "TestedThread";
     59 
     60     static Object waitForStart = new Object();
     61 
     62     @Override
     63     public void run() {
     64         DebuggeeThread thrd = new DebuggeeThread(TESTED_THREAD,
     65                 logWriter, synchronizer);
     66 
     67         synchronized(waitForStart){
     68             thrd.start();
     69             try {
     70                 waitForStart.wait();
     71             } catch (InterruptedException e) {
     72 
     73             }
     74         }
     75 
     76         logWriter.println("Wait for thread to finish...");
     77         while ( thrd.isAlive() ) {
     78             waitMlsecsTime(1000);
     79         }
     80         logWriter.println("thread finished");
     81         logWriter.println("send SGNL_READY");
     82         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
     83 
     84         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
     85     }
     86 
     87     static class DebuggeeThread extends Thread {
     88 
     89         LogWriter logWriter;
     90         DebuggeeSynchronizer synchronizer;
     91 
     92         public DebuggeeThread(String name, LogWriter logWriter,
     93                 DebuggeeSynchronizer synchronizer) {
     94             super(name);
     95             this.logWriter = logWriter;
     96             this.synchronizer = synchronizer;
     97         }
     98 
     99         @Override
    100         public void run() {
    101 
    102             logWriter.println(getName() +  ": started...");
    103             synchronized(AllThreadsDebuggee.waitForStart){
    104 
    105                 AllThreadsDebuggee.waitForStart.notifyAll();
    106             }
    107 
    108             synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
    109             logWriter.println(getName() +  ": wait for SGNL_CONTINUE");
    110             synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
    111             logWriter.println(getName() +  ": is finishing...");
    112         }
    113     }
    114 
    115     public static void main(String [] args) {
    116         runDebuggee(AllThreadsDebuggee.class);
    117     }
    118 }
    119