ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleWriteLockLoops.java
Revision: 1.7
Committed: Wed Dec 31 17:00:58 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
lexicographic import order

File Contents

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