ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleTimedLockLoops.java
Revision: 1.2
Committed: Fri Jun 10 15:45:19 2005 UTC (18 years, 11 months ago) by dl
Branch: MAIN
Changes since 1.1: +2 -2 lines
Log Message:
Add CAS timing test; use cheaper RNGs in others

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 SimpleTimedLockLoops {
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 = 10000000;
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 print = true;
27
28 int reps = 3;
29 for (int i = 1; i <= maxThreads; i += (i+1) >>> 1) {
30 int n = reps;
31 if (reps > 1) --reps;
32 while (n-- > 0) {
33 System.out.print("Threads: " + i);
34 new ReentrantLockLoop(i).test();
35 Thread.sleep(100);
36 }
37 }
38 pool.shutdown();
39 }
40
41 static final class ReentrantLockLoop implements Runnable {
42 private int v = rng.next();
43 private volatile int result = 17;
44 private final ReentrantLock lock = new ReentrantLock();
45 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
46 private final CyclicBarrier barrier;
47 private final int nthreads;
48 private volatile int readBarrier;
49 ReentrantLockLoop(int nthreads) {
50 this.nthreads = nthreads;
51 barrier = new CyclicBarrier(nthreads+1, timer);
52 }
53
54 final void test() throws Exception {
55 for (int i = 0; i < nthreads; ++i)
56 pool.execute(this);
57 barrier.await();
58 barrier.await();
59 if (print) {
60 long time = timer.getTime();
61 long tpi = time / ((long)iters * nthreads);
62 System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
63 double secs = (double)(time) / 1000000000.0;
64 System.out.println("\t " + secs + "s run time");
65 }
66
67 int r = result;
68 if (r == 0) // avoid overoptimization
69 System.out.println("useless result: " + r);
70 }
71
72 public final void run() {
73 final ReentrantLock lock = this.lock;
74 try {
75 barrier.await();
76 int sum = v + 1;
77 int x = 0;
78 int n = iters;
79 while (n > 0) {
80 if (lock.tryLock(1, TimeUnit.SECONDS)) {
81 --n;
82 int k = (sum & 3);
83 if (k > 0) {
84 x = v;
85 while (k-- > 0)
86 x = LoopHelpers.compute6(x);
87 v = x;
88 }
89 else x = sum + 1;
90 lock.unlock();
91 }
92 if ((x += readBarrier) == 0)
93 ++readBarrier;
94 for (int l = x & 7; l > 0; --l)
95 sum += LoopHelpers.compute6(sum);
96 }
97 barrier.await();
98 result += sum;
99 }
100 catch (Exception ie) {
101 return;
102 }
103 }
104 }
105 }