ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/NoopNoLockLoops.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

# User Rev Content
1 dl 1.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.concurrent.atomic.*;
14     import java.util.*;
15    
16     public final class NoopNoLockLoops {
17     static final ExecutorService pool = Executors.newCachedThreadPool();
18     static final LoopHelpers.SimpleRandom rng = new LoopHelpers.SimpleRandom();
19     static boolean print = false;
20     static int iters = 20000000;
21    
22     public static void main(String[] args) throws Exception {
23     int maxThreads = 100;
24     if (args.length > 0)
25     maxThreads = Integer.parseInt(args[0]);
26    
27     new ReentrantLockLoop(1).test();
28     new ReentrantLockLoop(1).test();
29     print = true;
30    
31     int k = 1;
32     for (int i = 1; i <= maxThreads;) {
33     System.out.print("Threads: " + i);
34     new ReentrantLockLoop(i).test();
35     Thread.sleep(100);
36     if (i == k) {
37     k = i << 1;
38     i = i + (i >>> 1);
39     }
40     else
41     i = k;
42     }
43     pool.shutdown();
44     }
45    
46     static final class ReentrantLockLoop implements Runnable {
47     private int v = rng.next();
48     private volatile int result = 17;
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     ReentrantLockLoop(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     try {
78     barrier.await();
79     int sum = v + 1;
80     int x = sum + 1;
81     int n = iters;
82     while (n-- > 0) {
83     x = LoopHelpers.compute4(x);
84     sum += x;
85     if ((x += readBarrier) == 0)
86     ++readBarrier;
87    
88     }
89     barrier.await();
90     result += sum;
91     }
92     catch (Exception ie) {
93     return;
94     }
95     }
96     }
97    
98     }
99