/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
*
* Sample target application for jvmti demos
*
* java Context [threadCount [iterationCount [sleepContention]]]
* Default: java Context 5 10 0
*
* threadCount Number of threads
* iterationCount Total turns taken for all threads
* sleepContention Time for main thread to sleep while holding lock
* (creates monitor contention on all other threads)
*
*/
/* Used to sync up turns and keep track of who's turn it is */
final class TurnChecker {
int thread_index;
this.thread_index = thread_index;
}
}
/* Creates a bunch of threads that sequentially take turns */
/* Used to track threads */
private static long startTime;
private static int total_turns_taken;
/* Used for each Context thread */
private final int thread_count;
private final int thread_index;
private final int thread_turns;
/* Main program */
int default_thread_count = 5;
int default_thread_turns = 10;
int default_contention_sleep = 0;
int expected_turns_taken;
long sleepTime = 10L;
/* Override defaults */
}
}
}
+ default_thread_count + " threads and "
+ default_thread_turns + " turns per thread");
/* Get all threads running (they will block until we set turn) */
for (int i = 0; i < default_thread_count; i++) {
}
/* Sleep to make sure thread_index 0 make it to the wait call */
/* Save start time */
/* This triggers the starting of taking turns */
synchronized (turn) {
}
/* Wait for threads to finish (after everyone has had their turns) */
while ( true ) {
boolean done;
done = false;
synchronized (turn) {
if ( total_turns_taken == expected_turns_taken ) {
done = true;
}
/* Create some monitor contention by sleeping with lock */
if ( default_contention_sleep > 0 ) {
}
}
if ( done )
break;
}
((double)totalTime / (default_thread_count)));
}
/* Thread object to run */
this.thread_count = thread_count;
this.thread_index = thread_index;
this.thread_turns = thread_turns;
}
/* Main for thread */
public void run() {
int turns_taken = 0;
try {
/* Loop until we make sure we get all our turns */
synchronized (turn) {
/* Keep waiting for our turn */
/* MY TURN! Each thread gets thread_turns */
turns_taken++;
+ " taken by thread " + thread_index
+ ", " + turns_taken
+ " turns taken by this thread");
/* Give next thread a turn */
}
/* If we've had all our turns, break out of this loop */
if ( thread_turns == turns_taken ) {
break;
}
}
/* Make sure we got all our turns */
if ( thread_turns != turns_taken ) {
+ " turns, expected " + thread_turns);
}
}
}