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 20.06.2006
25 */
26
27package org.apache.harmony.jpda.tests.jdwp.VirtualMachine;
28
29import org.apache.harmony.jpda.tests.share.SyncDebuggee;
30
31public class ResumeDebuggee extends SyncDebuggee {
32    public static final int THREAD_NUMBER_LIMIT = 10;
33    public static final String THREAD_NAME_PATTERN = "ResumeDebuggee_Thread_";
34    public static final String TO_FINISH_DEBUGGEE_FIELD_NAME = "debuggeToFinish";
35
36    static ResumeDebuggee ResumeDebuggeeThis;
37
38    static volatile boolean allThreadsToFinish = false;
39    static int debuggeToFinish = 0;
40    static int createdThreadsNumber = 0;
41    static volatile int startedThreadsNumber = 0;
42
43    static ResumeDebuggee_Thread[] ResumeDebuggeeThreads = null;
44
45    static Object waitTimeObject = new Object();
46    static void waitMlsecsTime(long mlsecsTime) {
47        synchronized(waitTimeObject) {
48            try {
49                waitTimeObject.wait(mlsecsTime);
50            } catch (Throwable throwable) {
51                 // ignore
52            }
53        }
54    }
55
56    static void sleepMlsecsTime(long mlsecsTime) {
57        try {
58            Thread.sleep(mlsecsTime);
59        } catch (Throwable throwable) {
60             // ignore
61        }
62    }
63
64    @Override
65    public void run() {
66
67        logWriter.println("--> ResumeDebuggee: START...");
68        ResumeDebuggeeThis = this;
69        logWriter.println("--> ResumeDebuggee: Create thread groups...");
70
71        logWriter.println("--> ResumeDebuggee: Create and start tested threads...");
72        try {
73            ResumeDebuggeeThreads = new ResumeDebuggee_Thread[THREAD_NUMBER_LIMIT];
74            for (int i=0; i < THREAD_NUMBER_LIMIT; i++) {
75                ResumeDebuggeeThreads[i] = new ResumeDebuggee_Thread(i);
76                ResumeDebuggeeThreads[i].start();
77                createdThreadsNumber++;
78            }
79        } catch ( Throwable thrown) {
80            logWriter.println
81            ("--> ResumeDebuggee: Exception while creating threads: " + thrown);
82        }
83        logWriter.println
84        ("--> ResumeDebuggee: Created threads number = " + createdThreadsNumber);
85
86        while ( startedThreadsNumber != createdThreadsNumber ) {
87            waitMlsecsTime(100);
88        }
89        if ( createdThreadsNumber != 0 ) {
90            logWriter.println("--> ResumeDebuggee: All created threads are started!");
91        }
92
93        synchronizer.sendMessage(Integer.toString(createdThreadsNumber));
94
95        String mainThreadName = Thread.currentThread().getName();
96        synchronizer.sendMessage(mainThreadName);
97
98        logWriter.println("--> ResumeDebuggee: Wait for signal from test to finish...");
99        while ( debuggeToFinish != 99 ) { // is set up by debugger - ResumeTest
100            waitMlsecsTime(100);
101        }
102
103        logWriter.println
104        ("--> ResumeDebuggee: Send signal to all threads to finish and wait...");
105        allThreadsToFinish = true;
106
107        for (int i=0; i < createdThreadsNumber; i++) {
108            while ( ResumeDebuggeeThreads[i].isAlive() ) {
109                waitMlsecsTime(10);
110            }
111        }
112        logWriter.println
113        ("--> ResumeDebuggee: All threads finished!");
114
115        logWriter.println("--> ResumeDebuggee: FINISH...");
116
117    }
118
119    public static void main(String [] args) {
120        runDebuggee(ResumeDebuggee.class);
121    }
122
123}
124
125class ResumeDebuggee_Thread extends Thread {
126
127    int threadKind;
128
129    public ResumeDebuggee_Thread(int threadNumber) {
130        super(ResumeDebuggee.THREAD_NAME_PATTERN + threadNumber);
131        threadKind = threadNumber % 3;
132    }
133
134    @Override
135    public void run() {
136        ResumeDebuggee parent = ResumeDebuggee.ResumeDebuggeeThis;
137        synchronized (parent) {
138            ResumeDebuggee.startedThreadsNumber++;
139        }
140        while ( ! ResumeDebuggee.allThreadsToFinish ) {
141            switch ( threadKind ) {
142            case 0:
143                ResumeDebuggee.waitMlsecsTime(100);
144                break;
145            case 1:
146                ResumeDebuggee.sleepMlsecsTime(100);
147            }
148        }
149    }
150}
151
152
153