ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ExchangeLoops.java
Revision: 1.3
Committed: Wed Apr 19 15:10:47 2006 UTC (18 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
Updated Navigable tests

File Contents

# Content
1 /*
2 * Written by Bill Scherer and Doug Lea with assistance from members
3 * of JCP JSR-166 Expert Group and released to the public domain. Use,
4 * modify, and redistribute this code in any way without
5 * acknowledgement.
6 */
7
8 import java.util.concurrent.*;
9 import java.util.concurrent.atomic.*;
10 import java.util.concurrent.locks.*;
11
12 public class ExchangeLoops {
13 static final int NCPUS = Runtime.getRuntime().availableProcessors();
14
15 static final int DEFAULT_THREADS = NCPUS + 2;
16 static final long DEFAULT_TRIAL_MILLIS = 10000;
17
18 public static void main(String[] args) throws Exception {
19 int maxThreads = DEFAULT_THREADS;
20 long trialMillis = DEFAULT_TRIAL_MILLIS;
21 int nReps = 3;
22
23 // Parse and check args
24 int argc = 0;
25 while (argc < args.length) {
26 String option = args[argc++];
27 if (option.equals("-t"))
28 trialMillis = Integer.parseInt(args[argc]);
29 else if (option.equals("-r"))
30 nReps = Integer.parseInt(args[argc]);
31 else
32 maxThreads = Integer.parseInt(option);
33 argc++;
34 }
35
36 // Display runtime parameters
37 System.out.print("ExchangeTest");
38 System.out.print(" -t " + trialMillis);
39 System.out.print(" -r " + nReps);
40 System.out.print(" max threads " + maxThreads);
41 System.out.println();
42 long warmupTime = 2000;
43 long sleepTime = 100;
44 int nw = maxThreads >= 3? 3 : 2;
45
46 System.out.println("Warmups..");
47 oneRun(3, warmupTime);
48 Thread.sleep(sleepTime);
49
50 for (int i = maxThreads; i >= 2; i -= 1) {
51 oneRun(i, warmupTime++);
52 // System.gc();
53 Thread.sleep(sleepTime);
54 }
55
56 /*
57 for (int i = maxThreads; i >= 2; i -= 1) {
58 oneRun(i, warmupTime++);
59 }
60 */
61
62 for (int j = 0; j < nReps; ++j) {
63 System.out.println("Trial: " + j);
64 for (int i = 2; i <= maxThreads; i += 2) {
65 oneRun(i, trialMillis);
66 // System.gc();
67 Thread.sleep(sleepTime);
68 }
69 for (int i = maxThreads; i >= 2; i -= 2) {
70 oneRun(i, trialMillis);
71 // System.gc();
72 Thread.sleep(sleepTime);
73 }
74 Thread.sleep(sleepTime);
75 }
76
77
78 }
79
80 static void oneRun(int nThreads, long trialMillis) throws Exception {
81 System.out.printf("%4d threads", nThreads);
82 Exchanger x = new Exchanger();
83 Runner[] runners = new Runner[nThreads];
84 Thread[] threads = new Thread[nThreads];
85 for (int i = 0; i < nThreads; ++i) {
86 runners[i] = new Runner(x);
87 threads[i] = new Thread(runners[i]);
88 // int h = System.identityHashCode(threads[i]);
89 // h ^= h << 1;
90 // h ^= h >>> 3;
91 // h ^= h << 10;
92 // System.out.printf("%10x\n", h);
93 }
94
95 long startTime = System.nanoTime();
96 for (int i = 0; i < nThreads; ++i) {
97 threads[i].start();
98 }
99 Thread.sleep(trialMillis);
100 for (int i = 0; i < nThreads; ++i)
101 threads[i].interrupt();
102 long elapsed = System.nanoTime() - startTime;
103 for (int i = 0; i < nThreads; ++i)
104 threads[i].join();
105 int iters = 1;
106 // System.out.println();
107 for (int i = 0; i < nThreads; ++i) {
108 int ipr = runners[i].iters;
109 // System.out.println(ipr);
110 iters += ipr;
111 }
112 long rate = iters * 1000L * 1000L * 1000L / elapsed;
113 long npt = elapsed / iters;
114 System.out.printf("%9dms", elapsed / (1000L * 1000L));
115 System.out.printf("%9d it/s ", rate);
116 System.out.printf("%9d ns/it", npt);
117 System.out.println();
118 // x.printStats();
119 }
120
121 static final class Runner implements Runnable {
122 final Exchanger exchanger;
123 final Object mine = new Integer(2688);
124 volatile int iters;
125 Runner(Exchanger x) { this.exchanger = x; }
126
127 public void run() {
128 Exchanger x = exchanger;
129 Object m = mine;
130 int i = 0;
131 try {
132 for (;;) {
133 Object e = x.exchange(m);
134 if (e == null || e == m)
135 throw new Error();
136 m = e;
137 ++i;
138 }
139 } catch (InterruptedException ie) {
140 iters = i;
141 }
142 }
143 }
144 }
145