ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/NoopLockLoops.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 builtin 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 NoopLockLoops {
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 = 20000000;
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 ReentrantLockLoop(1).test();
27 new ReentrantLockLoop(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 ReentrantLockLoop(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 ReentrantLockLoop implements Runnable {
46 private int v = rng.next();
47 private volatile int result = 17;
48 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
49 private final CyclicBarrier barrier;
50 private final int nthreads;
51 private volatile int readBarrier;
52 ReentrantLockLoop(int nthreads) {
53 this.nthreads = nthreads;
54 barrier = new CyclicBarrier(nthreads+1, timer);
55 }
56
57 final void test() throws Exception {
58 for (int i = 0; i < nthreads; ++i)
59 pool.execute(this);
60 barrier.await();
61 barrier.await();
62 if (print) {
63 long time = timer.getTime();
64 long tpi = time / ((long)iters * nthreads);
65 System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
66 double secs = (double)(time) / 1000000000.0;
67 System.out.println("\t " + secs + "s run time");
68 }
69
70 int r = result;
71 if (r == 0) // avoid overoptimization
72 System.out.println("useless result: " + r);
73 }
74
75 public final void run() {
76 try {
77 barrier.await();
78 int sum = v + 1;
79 int x = sum + 1;
80 int n = iters;
81 while (n-- > 0) {
82 synchronized(this) {
83 x = LoopHelpers.compute4(x);
84 }
85 sum += x;
86 if ((x += readBarrier) == 0)
87 ++readBarrier;
88
89 }
90 barrier.await();
91 result += sum;
92 }
93 catch (Exception ie) {
94 return;
95 }
96 }
97 }
98
99 }
100