ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleMutexLoops.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# Content
1 /*
2 * @test
3 * @synopsis multiple threads using a single lock
4 */
5 /*
6 * Written by Doug Lea with assistance from members of JCP JSR-166
7 * Expert Group and released to the public domain. Use, modify, and
8 * redistribute this code in any way without acknowledgement.
9 */
10
11 import java.util.concurrent.*;
12 import java.util.concurrent.locks.*;
13 import java.util.*;
14
15 public final class SimpleMutexLoops {
16 static final ExecutorService pool = Executors.newCachedThreadPool();
17 static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
18 static boolean print = false;
19 static int iters = 2000000;
20
21 public static void main(String[] args) throws Exception {
22 int maxThreads = 100;
23 if (args.length > 0)
24 maxThreads = Integer.parseInt(args[0]);
25
26 new MutexLoop(1).test();
27 new MutexLoop(1).test();
28 print = true;
29
30 int k = 1;
31 for (int i = 1; i <= maxThreads;) {
32 System.out.print("Threads: " + i);
33 new MutexLoop(i).test();
34 Thread.sleep(100);
35 if (i == k) {
36 k = i << 1;
37 i = i + (i >>> 1);
38 }
39 else
40 i = k;
41 }
42 pool.shutdown();
43 }
44
45 static final class MutexLoop implements Runnable {
46 private int v = rng.next();
47 private volatile int result = 17;
48 private final Mutex lock = new Mutex();
49 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
50 private final CyclicBarrier barrier;
51 private final int nthreads;
52 private volatile int readBarrier;
53 MutexLoop(int nthreads) {
54 this.nthreads = nthreads;
55 barrier = new CyclicBarrier(nthreads+1, timer);
56 }
57
58 final void test() throws Exception {
59 for (int i = 0; i < nthreads; ++i)
60 pool.execute(this);
61 barrier.await();
62 barrier.await();
63 if (print) {
64 long time = timer.getTime();
65 long tpi = time / ((long)iters * nthreads);
66 System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
67 double secs = (double)(time) / 1000000000.0;
68 System.out.println("\t " + secs + "s run time");
69 }
70
71 int r = result;
72 if (r == 0) // avoid overoptimization
73 System.out.println("useless result: " + r);
74 }
75
76 public final void run() {
77 final Mutex lock = this.lock;
78 try {
79 barrier.await();
80 int sum = v + 1;
81 int x = 0;
82 int n = iters;
83 while (n-- > 0) {
84 lock.lock();
85 int k = (sum & 3);
86 if (k > 0) {
87 x = v;
88 while (k-- > 0)
89 x = LoopHelpers.compute4(x);
90 v = x;
91 }
92 else x = sum + 1;
93 lock.unlock();
94 if ((x += readBarrier) == 0)
95 ++readBarrier;
96 for (int l = x & 7; l > 0; --l)
97 sum += LoopHelpers.compute4(sum);
98 }
99 barrier.await();
100 result += sum;
101 }
102 catch (Exception ie) {
103 return;
104 }
105 }
106 }
107
108 }