ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RLJBar.java
Revision: 1.7
Committed: Thu Apr 14 23:16:10 2011 UTC (13 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0
Changes since 1.6: +102 -103 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.2 /*
2     * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.6 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.2 */
6    
7 dl 1.1 // Yet another contended object monitor throughput test
8     // adapted from bug reports
9    
10     import java.util.*;
11     import java.lang.*;
12     import java.util.concurrent.*;
13     import java.util.concurrent.locks.*;
14 jsr166 1.3
15     class Producer extends Thread
16 dl 1.1 {
17     // private static Hashtable buddiesOnline = new Hashtable();
18 jsr166 1.7 private static Map buddiesOnline = new ConcurrentHashMap();
19     public Producer(String name) { super(name); }
20 dl 1.1
21 jsr166 1.7 public void run() {
22     Object key = null;
23     final ReentrantLock dr = RLJBar.DeathRow;
24     final ReentrantLock bar = RLJBar.bar;
25     final ReentrantLock end = RLJBar.End;
26     final Condition endCondition = RLJBar.EndCondition;
27     if (RLJBar.OneKey) key = new Integer(0); // per-thread v. per iteration
28    
29     // The barrier has a number of interesting effects:
30     // 1. It enforces full LWP provisioning on T1.
31     // (nearly all workers park concurrently).
32     // 2. It gives the C2 compiler thread(s) a chance to run.
33     // By transiently quiescing the workings the C2 threads
34     // might avoid starvation.
35     //
36    
37     try {
38     bar.lock();
39     try {
40     ++RLJBar.nUp;
41     if (RLJBar.nUp == RLJBar.nThreads) {
42     if (RLJBar.quiesce != 0) {
43     RLJBar.barCondition.awaitNanos(RLJBar.quiesce * 1000000);
44     }
45     RLJBar.epoch = System.currentTimeMillis();
46     RLJBar.barCondition.signalAll();
47     // System.out.print ("Consensus ");
48     }
49     if (RLJBar.UseBar) {
50     while (RLJBar.nUp != RLJBar.nThreads) {
51     RLJBar.barCondition.await();
52     }
53     }
54     }
55     finally {
56     bar.unlock();
57     }
58     } catch (Exception ex) {
59     System.out.println("Exception in barrier: " + ex);
60     }
61    
62     // Main execution time ... the code being timed ...
63     // HashTable.get() is highly contended (serial).
64     for (int loop = 1; loop < 100000 ;loop++) {
65     if (!RLJBar.OneKey) key = new Integer(0);
66     buddiesOnline.get(key);
67     }
68    
69     // Mutator epilog:
70     // The following code determines if the test will/wont include (measure)
71     // thread death time.
72    
73     end.lock();
74     try {
75     ++RLJBar.nDead;
76     if (RLJBar.nDead == RLJBar.nUp) {
77     // System.out.print((System.currentTimeMillis()-RLJBar.epoch) + " ms");
78     endCondition.signalAll();
79     }
80     }
81     finally {
82     end.unlock();
83     }
84     dr.lock();
85     dr.unlock();
86 dl 1.1 }
87     }
88    
89    
90 jsr166 1.4 public class RLJBar // ProdConsTest
91 dl 1.1 {
92    
93     public static final int ITERS = 10;
94 jsr166 1.7 public static boolean OneKey = false; // alloc once or once per iteration
95 dl 1.1
96 jsr166 1.7 public static boolean UseBar = false;
97     public static int nThreads = 100;
98     public static int nUp = 0;
99     public static int nDead = 0;
100     public static ReentrantLock bar = new ReentrantLock();
101     public static Condition barCondition = bar.newCondition();
102     public static long epoch;
103     public static ReentrantLock DeathRow = new ReentrantLock();
104     public static ReentrantLock End = new ReentrantLock();
105     public static int quiesce = 0;
106     public static Condition EndCondition = End.newCondition();
107    
108     public static void main(String[] args) {
109     int argix = 0;
110     if (argix < args.length && args[argix].equals("-o")) {
111     ++argix;
112     OneKey = true;
113     System.out.println("OneKey");
114     }
115     if (argix < args.length && args[argix].equals ("-b")) {
116     ++argix;
117     UseBar = true;
118     System.out.println("UseBar");
119     }
120     if (argix < args.length && args[argix].equals ("-q")) {
121     ++argix;
122     if (argix < args.length) {
123     quiesce = Integer.parseInt(args[argix++]);
124     System.out.println("Quiesce " + quiesce + " msecs");
125     }
126     }
127     for (int k = 0; k < ITERS; ++k)
128     oneRun();
129     }
130 dl 1.1
131     public static void oneRun() {
132 jsr166 1.5 DeathRow = new ReentrantLock();
133     End = new ReentrantLock();
134 dl 1.1 EndCondition = End.newCondition();
135 jsr166 1.3
136 jsr166 1.5 nDead = nUp = 0;
137     long cyBase = System.currentTimeMillis();
138 dl 1.1 DeathRow.lock();
139     try {
140 jsr166 1.5 for (int i = 1; i <= nThreads; i++) {
141 dl 1.1 new Producer("Producer" + i).start();
142     }
143 jsr166 1.3 try {
144 dl 1.1 End.lock();
145     try {
146 jsr166 1.3 while (nDead != nThreads)
147 jsr166 1.5 EndCondition.await();
148 dl 1.1 }
149     finally {
150     End.unlock();
151     }
152 jsr166 1.3 } catch (Exception ex) {
153 jsr166 1.5 System.out.println("Exception in End: " + ex);
154 dl 1.1 }
155     }
156     finally {
157     DeathRow.unlock();
158     }
159 jsr166 1.5 System.out.println("Outer time: " + (System.currentTimeMillis()-cyBase));
160 jsr166 1.3
161     // Let workers quiesce/exit.
162 jsr166 1.5 try { Thread.sleep (1000); } catch (Exception ex) {};
163 dl 1.1 }
164     }