Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/demo/jvmti/Context.java
38833 views
/*1* Copyright (c) 2004, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/222324/*25*26* Sample target application for jvmti demos27*28* java Context [threadCount [iterationCount [sleepContention]]]29* Default: java Context 5 10 030*31* threadCount Number of threads32* iterationCount Total turns taken for all threads33* sleepContention Time for main thread to sleep while holding lock34* (creates monitor contention on all other threads)35*36*/3738/* Used to sync up turns and keep track of who's turn it is */39final class TurnChecker {40int thread_index;41TurnChecker(int thread_index) {42this.thread_index = thread_index;43}44}4546/* Creates a bunch of threads that sequentially take turns */47public final class Context extends Thread {48/* Used to track threads */49private static long startTime;50private static TurnChecker turn = new TurnChecker(-1);51private static int total_turns_taken;5253/* Used for each Context thread */54private final int thread_count;55private final int thread_index;56private final int thread_turns;5758/* Main program */59public static void main(String[] argv) throws InterruptedException {60int default_thread_count = 5;61int default_thread_turns = 10;62int default_contention_sleep = 0;63int expected_turns_taken;64long sleepTime = 10L;6566/* Override defaults */67if ( argv.length >= 1 ) {68default_thread_count = Integer.parseInt(argv[0]);69}70if ( argv.length >= 2 ) {71expected_turns_taken = Integer.parseInt(argv[1]);72default_thread_turns = expected_turns_taken/default_thread_count;73}74expected_turns_taken = default_thread_count*default_thread_turns;75if ( argv.length >= 3 ) {76default_contention_sleep = Integer.parseInt(argv[2]);77}7879System.out.println("Context started with "80+ default_thread_count + " threads and "81+ default_thread_turns + " turns per thread");8283/* Get all threads running (they will block until we set turn) */84for (int i = 0; i < default_thread_count; i++) {85new Context(default_thread_count, i, default_thread_turns).start();86}8788/* Sleep to make sure thread_index 0 make it to the wait call */89System.out.println("Context sleeping, so threads will start wait");90Thread.yield();91Thread.sleep(sleepTime);9293/* Save start time */94startTime = System.currentTimeMillis();9596/* This triggers the starting of taking turns */97synchronized (turn) {98turn.thread_index = 0;99turn.notifyAll();100}101System.out.println("Context sleeping, so threads can run");102Thread.yield();103Thread.sleep(sleepTime);104105/* Wait for threads to finish (after everyone has had their turns) */106while ( true ) {107boolean done;108done = false;109synchronized (turn) {110if ( total_turns_taken == expected_turns_taken ) {111done = true;112}113/* Create some monitor contention by sleeping with lock */114if ( default_contention_sleep > 0 ) {115System.out.println("Context sleeping, to create contention");116Thread.yield();117Thread.sleep((long)default_contention_sleep);118}119}120if ( done )121break;122System.out.println("Context sleeping, so threads will complete");123Thread.sleep(sleepTime);124}125126long endTime = System.currentTimeMillis();127long totalTime = endTime - startTime;128129System.out.println("Total time (milliseconds): " + totalTime);130System.out.println("Milliseconds per thread: " +131((double)totalTime / (default_thread_count)));132133System.out.println("Context completed");134System.exit(0);135}136137/* Thread object to run */138Context(int thread_count, int thread_index, int thread_turns) {139this.thread_count = thread_count;140this.thread_index = thread_index;141this.thread_turns = thread_turns;142}143144/* Main for thread */145public void run() {146int next_thread_index = (thread_index + 1) % thread_count;147int turns_taken = 0;148149try {150151/* Loop until we make sure we get all our turns */152for (int i = 0; i < thread_turns * thread_count; i++) {153synchronized (turn) {154/* Keep waiting for our turn */155while (turn.thread_index != thread_index)156turn.wait();157/* MY TURN! Each thread gets thread_turns */158total_turns_taken++;159turns_taken++;160System.out.println("Turn #" + total_turns_taken161+ " taken by thread " + thread_index162+ ", " + turns_taken163+ " turns taken by this thread");164/* Give next thread a turn */165turn.thread_index = next_thread_index;166turn.notifyAll();167}168/* If we've had all our turns, break out of this loop */169if ( thread_turns == turns_taken ) {170break;171}172}173} catch (InterruptedException intEx) { /* skip */ }174175/* Make sure we got all our turns */176if ( thread_turns != turns_taken ) {177System.out.println("ERROR: thread got " + turns_taken178+ " turns, expected " + thread_turns);179System.exit(1);180}181}182}183184185