ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/RLJBar.java
Revision: 1.4
Committed: Wed Sep 1 07:20:36 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +8 -8 lines
Log Message:
untabify

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