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 Anatoly F. Bondarenko
21  */
22 
23 /**
24  * Created on 19.06.2006
25  */
26 
27 package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
28 
29 import org.apache.harmony.jpda.tests.share.JPDADebuggeeSynchronizer;
30 import org.apache.harmony.jpda.tests.share.SyncDebuggee;
31 
32 public class ThreadGroup002Debuggee extends SyncDebuggee {
33     public static final int THREAD_NUMBER_LIMIT = 6;
34     public static final String THREAD_NAME_PATTERN = "ThreadGroup002Debuggee_Thread_";
35     public static final String THREAD_GROUP_NAME_PATTERN = "ThreadGroup002Debuggee_Thread_Group_";
36 
37     static ThreadGroup002Debuggee ThreadGroup002DebuggeeThis;
38 
39     static volatile boolean allThreadsToFinish = false;
40     static int createdThreadsNumber = 0;
41     static volatile int startedThreadsNumber = 0;
42 
43     static int firstThreadsNumber = 0;
44 
45     static ThreadGroup002Debuggee_Thread[] ThreadGroup002DebuggeeThreads = null;
46     static ThreadGroup[] ThreadGroup002DebuggeeThreadGroups = new ThreadGroup[2];
47 
48     static Object waitTimeObject = new Object();
waitMlsecsTime(long mlsecsTime)49     static void waitMlsecsTime(long mlsecsTime) {
50         synchronized(waitTimeObject) {
51             try {
52                 waitTimeObject.wait(mlsecsTime);
53             } catch (Throwable throwable) {
54                  // ignore
55             }
56         }
57     }
58 
sleepMlsecsTime(long mlsecsTime)59     static void sleepMlsecsTime(long mlsecsTime) {
60         try {
61             Thread.sleep(mlsecsTime);
62         } catch (Throwable throwable) {
63              // ignore
64         }
65     }
66 
run()67     public void run() {
68 
69         logWriter.println("--> ThreadGroup002Debuggee: START...");
70         ThreadGroup002DebuggeeThis = this;
71         logWriter.println("--> ThreadGroup002Debuggee: Create thread groups...");
72         ThreadGroup002DebuggeeThreadGroups[0] =
73             new ThreadGroup(THREAD_GROUP_NAME_PATTERN + 0);
74         ThreadGroup002DebuggeeThreadGroups[1] =
75             new ThreadGroup(THREAD_GROUP_NAME_PATTERN + 1);
76 
77         logWriter.println("--> ThreadGroup002Debuggee: Create and start tested threads...");
78         try {
79             ThreadGroup002DebuggeeThreads = new ThreadGroup002Debuggee_Thread[THREAD_NUMBER_LIMIT];
80             for (int i=0; i < THREAD_NUMBER_LIMIT; i++) {
81                 ThreadGroup002DebuggeeThreads[i] =
82                     new ThreadGroup002Debuggee_Thread(ThreadGroup002DebuggeeThreadGroups[i%2], i);
83                 ThreadGroup002DebuggeeThreads[i].start();
84                 createdThreadsNumber++;
85             }
86         } catch ( Throwable thrown) {
87             logWriter.println
88             ("--> ThreadGroup002Debuggee: Exception while creating threads: " + thrown);
89         }
90         logWriter.println
91         ("--> ThreadGroup002Debuggee: Created threads number = " + createdThreadsNumber);
92 
93         while ( startedThreadsNumber != createdThreadsNumber ) {
94             waitMlsecsTime(100);
95         }
96         if ( createdThreadsNumber != 0 ) {
97             logWriter.println("--> ThreadGroup002Debuggee: All created threads are started!");
98         }
99 
100         synchronizer.sendMessage(Integer.toString(createdThreadsNumber));
101 
102         String mainThreadName = Thread.currentThread().getName();
103         synchronizer.sendMessage(mainThreadName);
104 
105         String mainThreadGroupName = Thread.currentThread().getThreadGroup().getName();
106         synchronizer.sendMessage(mainThreadGroupName);
107 
108 
109         logWriter.println("--> ThreadGroup002Debuggee: Wait for signal from test to continue...");
110         String messageFromTest = synchronizer.receiveMessage();  // signal to continue or to finish
111 
112         if ( ! messageFromTest.equals("FINISH") ) {
113             logWriter.println
114             ("--> ThreadGroup002Debuggee: Send signal to the first threads to finish...");
115 
116             firstThreadsNumber = createdThreadsNumber/2;
117             for (int i=0; i < firstThreadsNumber; i++) {
118                 while ( ThreadGroup002DebuggeeThreads[i].isAlive() ) {
119                     waitMlsecsTime(10);
120                 }
121             }
122             logWriter.println
123             ("--> ThreadGroup002Debuggee: First threads finished - number of finished threads = " +
124                     firstThreadsNumber);
125 
126             synchronizer.sendMessage(JPDADebuggeeSynchronizer.SGNL_READY);
127 
128             logWriter.println("--> ThreadGroup002Debuggee: Wait for signal from test to finish...");
129             synchronizer.receiveMessage();  // signal to finish
130         }
131 
132         logWriter.println
133         ("--> ThreadGroup002Debuggee: Send signal to all threads to finish and wait...");
134         allThreadsToFinish = true;
135 
136         for (int i=0; i < createdThreadsNumber; i++) {
137             while ( ThreadGroup002DebuggeeThreads[i].isAlive() ) {
138                 waitMlsecsTime(10);
139             }
140         }
141         logWriter.println
142         ("--> ThreadGroup002Debuggee: All threads finished!");
143 
144         logWriter.println("--> ThreadGroup002Debuggee: FINISH...");
145 
146     }
147 
main(String [] args)148     public static void main(String [] args) {
149         runDebuggee(ThreadGroup002Debuggee.class);
150     }
151 
152 }
153 
154 class ThreadGroup002Debuggee_Thread extends Thread {
155 
156     int threadNumber;
157     int threadKind;
158 
ThreadGroup002Debuggee_Thread(ThreadGroup group, int threadNumber)159     public ThreadGroup002Debuggee_Thread(ThreadGroup group, int threadNumber) {
160         super(group, ThreadGroup002Debuggee.THREAD_NAME_PATTERN + threadNumber);
161         this.threadNumber = threadNumber;
162         threadKind = threadNumber % 3;
163     }
164 
run()165     public void run() {
166         ThreadGroup002Debuggee parent = ThreadGroup002Debuggee.ThreadGroup002DebuggeeThis;
167         synchronized (parent) {
168             ThreadGroup002Debuggee.startedThreadsNumber++;
169         }
170         while ( ! ThreadGroup002Debuggee.allThreadsToFinish ) {
171             switch ( threadKind ) {
172             case 0:
173                 ThreadGroup002Debuggee.waitMlsecsTime(100);
174                 break;
175             case 1:
176                 ThreadGroup002Debuggee.sleepMlsecsTime(100);
177             }
178             if (threadNumber < ThreadGroup002Debuggee.firstThreadsNumber ) {
179                 return;
180             }
181         }
182     }
183 }
184 
185 
186