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 08.06.2006
25 */
26
27package org.apache.harmony.jpda.tests.jdwp.ThreadReference;
28
29import org.apache.harmony.jpda.tests.share.SyncDebuggee;
30
31public class ResumeDebuggee extends SyncDebuggee {
32    public static final int THREAD_NUMBER_LIMIT = 6;
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    public void run() {
65
66        logWriter.println("--> ResumeDebuggee: START...");
67        resumeDebuggeeThis = this;
68
69        logWriter.println("--> ResumeDebuggee: Create and start tested threads...");
70        try {
71            resumeDebuggeeThreads = new ResumeDebuggee_Thread[THREAD_NUMBER_LIMIT];
72            for (int i=0; i < THREAD_NUMBER_LIMIT; i++) {
73                resumeDebuggeeThreads[i]= new ResumeDebuggee_Thread(i);
74                resumeDebuggeeThreads[i].start();
75                createdThreadsNumber++;
76            }
77        } catch ( Throwable thrown) {
78            logWriter.println
79            ("--> ResumeDebuggee: Exception while creating threads: " + thrown);
80        }
81        logWriter.println
82        ("--> ResumeDebuggee: Created threads number = " + createdThreadsNumber);
83
84        while ( startedThreadsNumber != createdThreadsNumber ) {
85            waitMlsecsTime(100);
86        }
87        if ( createdThreadsNumber != 0 ) {
88            logWriter.println("--> ResumeDebuggee: All created threads are started!");
89        }
90
91        synchronizer.sendMessage(Integer.toString(createdThreadsNumber));
92
93        String mainThreadName = Thread.currentThread().getName();
94        synchronizer.sendMessage(mainThreadName);
95
96        logWriter.println("--> ResumeDebuggee: Wait for signal from test to finish...");
97        while ( debuggeToFinish != 99 ) { // is set up by debugger - ResumeTest
98            waitMlsecsTime(100);
99        }
100
101        logWriter.println
102        ("--> ResumeDebuggee: Send signal to all threads to finish and wait...");
103        allThreadsToFinish = true;
104
105        for (int i=0; i < createdThreadsNumber; i++) {
106            while ( resumeDebuggeeThreads[i].isAlive() ) {
107                waitMlsecsTime(10);
108            }
109        }
110        logWriter.println
111        ("--> ResumeDebuggee: All threads finished!");
112
113        logWriter.println("--> ResumeDebuggee: FINISH...");
114
115    }
116
117    public static void main(String [] args) {
118        runDebuggee(ResumeDebuggee.class);
119    }
120
121}
122
123class ResumeDebuggee_Thread extends Thread {
124
125    int threadKind;
126
127    public ResumeDebuggee_Thread(int threadNumber) {
128        super(ResumeDebuggee.THREAD_NAME_PATTERN + threadNumber);
129        threadKind = threadNumber % 3;
130    }
131
132    public void run() {
133        ResumeDebuggee parent = ResumeDebuggee.resumeDebuggeeThis;
134        synchronized (parent) {
135            ResumeDebuggee.startedThreadsNumber++;
136        }
137        while ( ! ResumeDebuggee.allThreadsToFinish ) {
138            switch ( threadKind ) {
139            case 0:
140                ResumeDebuggee.waitMlsecsTime(100);
141                break;
142            case 1:
143                ResumeDebuggee.sleepMlsecsTime(100);
144            }
145        }
146    }
147}
148
149
150