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