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 package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
20 
21 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
22 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
23 
24 /**
25  * Debuggee for Resume002Test.
26  */
27 public class Resume002Debuggee extends SyncDebuggee {
28     public static final int THREAD_COUNT = 5;
29 
30     // The method used to suspend threads on breakpoint.
breakpointMethod()31     public static void breakpointMethod() {
32         String threadName = Thread.currentThread().getName();
33         System.out.println(threadName + " enters breakpointMethod");
34     }
35 
36     class ResumeThread extends Thread {
ResumeThread(int i)37         public ResumeThread(int i) {
38             super("ResumeThread-" + i);
39         }
40 
41         @Override
run()42         public void run() {
43             String threadName = Thread.currentThread().getName();
44             logWriter.println("Thread \"" + threadName + "\" starts");
45             breakpointMethod();
46             logWriter.println("Thread \"" + threadName + "\" ends");
47         }
48 
49     }
50 
51     @Override
run()52     public void run() {
53         // Tell the debugger we're ready.
54         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
55 
56         // Wait for the debugger to install breakpoint.
57         synchronizer.receiveMessage(JPDADebuggeeSynchronizer.SGNL_CONTINUE);
58 
59         // Create tested threads.
60         Thread[] threads = new Thread[THREAD_COUNT];
61         for (int i = 0; i < threads.length; ++i) {
62             threads[i] = new ResumeThread(i);
63         }
64 
65         // Start threads.
66         logWriter.println("Starting threads");
67         for (Thread t : threads) {
68             t.start();
69         }
70 
71         // Wait for all tested threads to finish.
72         logWriter.println("Waiting end of threads");
73         for (Thread t : threads) {
74             try {
75                 t.join();
76             } catch (InterruptedException e) {
77                 logWriter.printError("Join failed", e);
78             }
79         }
80         logWriter.println("All threads terminated");
81 
82         // Tell the debugger all threads terminated.
83         synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
84     }
85 
main(String[] args)86     public static void main(String[] args) {
87         runDebuggee(Resume002Debuggee.class);
88     }
89 
90 }
91