ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/SimpleLoops.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
11 public final class SimpleLoops {
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 if (args.length > 0)
20 maxThreads = Integer.parseInt(args[0]);
21
22 print = true;
23
24 for (int k = 1, i = 1; i <= maxThreads;) {
25 System.out.print("Threads: " + 1);
26 new Loop(1).test();
27 Thread.sleep(100);
28 if (i == k) {
29 k = i << 1;
30 i = i + (i >>> 1);
31 }
32 else
33 i = k;
34 }
35 pool.shutdown();
36 }
37
38 static final class Loop implements Runnable {
39 private int v = rng.next();
40 private volatile int result = 17;
41 private final LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
42 private final CyclicBarrier barrier;
43 private final int nthreads;
44 private volatile int readBarrier;
45 Loop(int nthreads) {
46 this.nthreads = nthreads;
47 barrier = new CyclicBarrier(nthreads+1, timer);
48 }
49
50 final void test() throws Exception {
51 for (int i = 0; i < nthreads; ++i)
52 pool.execute(this);
53 barrier.await();
54 barrier.await();
55 if (print) {
56 long time = timer.getTime();
57 long tpi = time / ((long) iters * nthreads);
58 System.out.print("\t" + LoopHelpers.rightJustify(tpi) + " ns per lock");
59 double secs = (double) time / 1000000000.0;
60 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 try {
70 barrier.await();
71 int sum = v + 1;
72 int x = 0;
73 int n = iters;
74 while (n-- > 0) {
75 int k = (sum & 3);
76 if (k > 0) {
77 x = v;
78 while (k-- > 0)
79 x = LoopHelpers.compute1(x);
80 v = x;
81 }
82 else x = sum + 1;
83 if ((x += readBarrier) == 0)
84 ++readBarrier;
85 for (int l = x & 7; l > 0; --l)
86 sum += LoopHelpers.compute1(sum);
87 }
88 barrier.await();
89 result += sum;
90 }
91 catch (Exception ie) {
92 return;
93 }
94 }
95 }
96
97 }