1 /*
   2  * Copyright (c) 2004, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 

  24 /*
  25  *
  26  *   Sample target application for jvmti demos
  27  *
  28  *     java Context [threadCount [iterationCount [sleepContention]]]
  29  *           Default: java Context 5 10 0
  30  *
  31  *      threadCount     Number of threads
  32  *      iterationCount  Total turns taken for all threads
  33  *      sleepContention Time for main thread to sleep while holding lock
  34  *                      (creates monitor contention on all other threads)
  35  *
  36  */
  37 
  38 import java.util.concurrent.atomic.AtomicInteger;
  39 
  40 /* Used to sync up turns and keep track of who's turn it is */
  41 final class TurnChecker {
  42     int thread_index;
  43     TurnChecker(int thread_index) {
  44         this.thread_index = thread_index;
  45     }
  46 }
  47 
  48 /* Creates a bunch of threads that sequentially take turns */
  49 public final class Context extends Thread {
  50     /* Used to track threads */
  51     private static long startTime;


  52 
  53     /* Used for each Context thread */
  54     private final int thread_count;
  55     private final int thread_index;
  56     private final int thread_turns;
  57     private final TurnChecker turn;
  58     private final AtomicInteger total_turns_taken;
  59 
  60     /* Main program */
  61     public static void main(String[] argv) throws InterruptedException {
  62         int default_thread_count = 5;
  63         int default_thread_turns = 10;
  64         int default_contention_sleep = 0;
  65         AtomicInteger total_turns_taken = new AtomicInteger(0);
  66         TurnChecker turn = new TurnChecker(-1);
  67         int expected_turns_taken;
  68         long sleepTime = 10L;
  69 
  70         /* Override defaults */
  71         if ( argv.length >= 1 ) {
  72             default_thread_count = Integer.parseInt(argv[0]);
  73         }
  74         if ( argv.length >= 2 ) {
  75             expected_turns_taken = Integer.parseInt(argv[1]);
  76             default_thread_turns = expected_turns_taken/default_thread_count;
  77         }
  78         expected_turns_taken = default_thread_count*default_thread_turns;
  79         if ( argv.length >= 3 ) {
  80             default_contention_sleep = Integer.parseInt(argv[2]);
  81         }
  82 
  83         System.out.println("Context started with "
  84                  + default_thread_count + " threads and "
  85                  + default_thread_turns + " turns per thread");
  86 
  87         /* Get all threads running (they will block until we set turn) */
  88         for (int i = 0; i < default_thread_count; i++) {
  89             new Context(default_thread_count, i, default_thread_turns,
  90                     total_turns_taken, turn).start();
  91         }
  92 
  93         /* Sleep to make sure thread_index 0 make it to the wait call */
  94         System.out.println("Context sleeping, so threads will start wait");

  95         Thread.sleep(sleepTime);
  96 
  97         /* Save start time */
  98         startTime = System.currentTimeMillis();
  99 
 100         /* This triggers the starting of taking turns */
 101         synchronized (turn) {
 102             turn.thread_index = 0;
 103             turn.notifyAll();
 104         }
 105         System.out.println("Context sleeping, so threads can run");

 106         Thread.sleep(sleepTime);
 107 
 108         /* Wait for threads to finish (after everyone has had their turns) */
 109         while ( true ) {
 110             boolean done;
 111             done = false;
 112             synchronized (turn) {
 113                 int current_total_turns_taken = total_turns_taken.get();
 114                 System.out.println("total_turns_taken =" +
 115                         current_total_turns_taken +
 116                         ";expected_turns_taken =" +
 117                         expected_turns_taken);
 118                 if ( current_total_turns_taken == expected_turns_taken ) {
 119                     done = true;
 120                 }
 121                 /* Create some monitor contention by sleeping with lock */
 122                 if ( default_contention_sleep > 0 ) {
 123                     System.out.println("Context sleeping, to create contention");

 124                     Thread.sleep((long)default_contention_sleep);
 125                 }
 126             }
 127             if ( done )
 128                 break;
 129             System.out.println("Context sleeping, so threads will complete");
 130             Thread.sleep(sleepTime);
 131         }
 132 
 133         long endTime   = System.currentTimeMillis();
 134         long totalTime = endTime - startTime;
 135 
 136         System.out.println("Total time (milliseconds): " + totalTime);
 137         System.out.println("Milliseconds per thread: " +
 138                            ((double)totalTime / (default_thread_count)));
 139 
 140         System.out.println("Context completed");
 141         System.exit(0);
 142     }
 143 
 144     /* Thread object to run */
 145     Context(int thread_count, int thread_index, int thread_turns,
 146             AtomicInteger total_turns_taken, TurnChecker turn) {
 147         this.thread_count = thread_count;
 148         this.thread_index = thread_index;
 149         this.thread_turns = thread_turns;
 150         this.total_turns_taken = total_turns_taken;
 151         this.turn = turn;
 152     }
 153 
 154     /* Main for thread */
 155     @Override
 156     public void run() {
 157         int next_thread_index = (thread_index + 1) % thread_count;
 158         int turns_taken       = 0;
 159 
 160         try {
 161 
 162             /* Loop until we make sure we get all our turns */
 163             for (int i = 0; i < thread_turns * thread_count; i++) {
 164                 synchronized (turn) {
 165                     /* Keep waiting for our turn */
 166                     while (turn.thread_index != thread_index)
 167                         turn.wait();
 168                     /* MY TURN! Each thread gets thread_turns */

 169                     turns_taken++;
 170                     System.out.println("Turn #"
 171                                 + total_turns_taken.incrementAndGet()
 172                                 + " taken by thread " + thread_index
 173                                 + ", " + turns_taken
 174                                 + " turns taken by this thread");
 175                     /* Give next thread a turn */
 176                     turn.thread_index = next_thread_index;
 177                     turn.notifyAll();
 178                 }
 179                 /* If we've had all our turns, break out of this loop */
 180                 if ( thread_turns == turns_taken ) {
 181                     System.out.println("Loop end: thread got " + turns_taken
 182                                         + " turns, expected " + thread_turns);
 183                     break;
 184                 }
 185             }
 186         } catch (InterruptedException intEx) {
 187             System.out.println("Got an InterruptedException:" + intEx);
 188             /* skip */ 
 189         }
 190 
 191         /* Make sure we got all our turns */
 192         if ( thread_turns != turns_taken ) {
 193             System.out.println("ERROR: thread got " + turns_taken
 194                                         + " turns, expected " + thread_turns);
 195             System.exit(1);
 196         }
 197     }
 198 }
--- EOF ---