ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleSemaphoreLoops.java
Revision: 1.8
Committed: Sat Dec 31 19:05:06 2016 UTC (7 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +4 -3 lines
Log Message:
organize imports

File Contents

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