ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jsr166e/DoubleAdderDemo.java
Revision: 1.3
Committed: Thu Feb 26 06:53:34 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +0 -1 lines
Log Message:
delete unused 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.Phaser;
8 import java.util.concurrent.ExecutorService;
9 import java.util.concurrent.Executors;
10 import jsr166e.DoubleAdder;
11
12 public class DoubleAdderDemo {
13 static final int INCS_PER_THREAD = 10000000;
14 static final int NCPU = Runtime.getRuntime().availableProcessors();
15 static final ExecutorService pool = Executors.newCachedThreadPool();
16
17 static final class SynchronizedDoubleAdder {
18 double value;
19 synchronized double sum() { return value; }
20 synchronized void add(double x) { value += x; }
21 }
22
23 public static void main(String[] args) {
24 System.out.println("Warmup...");
25 int half = NCPU > 1 ? NCPU / 2 : 1;
26 syncTest(half, 1000);
27 adderTest(half, 1000);
28
29 for (int reps = 0; reps < 2; ++reps) {
30 System.out.println("Running...");
31 for (int i = 1; i <= NCPU * 2; i <<= 1) {
32 syncTest(i, INCS_PER_THREAD);
33 adderTest(i, INCS_PER_THREAD);
34 }
35 }
36 pool.shutdown();
37 }
38
39 static void syncTest(int nthreads, int incs) {
40 System.out.print("Synchronized ");
41 Phaser phaser = new Phaser(nthreads + 1);
42 SynchronizedDoubleAdder a = new SynchronizedDoubleAdder();
43 for (int i = 0; i < nthreads; ++i)
44 pool.execute(new SyncTask(a, phaser, incs));
45 report(nthreads, incs, timeTasks(phaser), a.sum());
46 }
47
48 static void adderTest(int nthreads, int incs) {
49 System.out.print("DoubleAdder ");
50 Phaser phaser = new Phaser(nthreads + 1);
51 DoubleAdder a = new DoubleAdder();
52 for (int i = 0; i < nthreads; ++i)
53 pool.execute(new AdderTask(a, phaser, incs));
54 report(nthreads, incs, timeTasks(phaser), a.sum());
55 }
56
57 static void report(int nthreads, int incs, long time, double sum) {
58 long total = (long)nthreads * incs;
59 if (sum != (double)total)
60 throw new Error(sum + " != " + total);
61 double secs = (double)time / (1000L * 1000 * 1000);
62 long rate = total * (1000L) / time;
63 System.out.printf("threads:%3d Time: %7.3fsec Incs per microsec: %4d\n",
64 nthreads, secs, rate);
65 }
66
67 static long timeTasks(Phaser phaser) {
68 phaser.arriveAndAwaitAdvance();
69 long start = System.nanoTime();
70 phaser.arriveAndAwaitAdvance();
71 phaser.arriveAndAwaitAdvance();
72 return System.nanoTime() - start;
73 }
74
75 static final class AdderTask implements Runnable {
76 final DoubleAdder adder;
77 final Phaser phaser;
78 final int incs;
79 volatile double result;
80 AdderTask(DoubleAdder adder, Phaser phaser, int incs) {
81 this.adder = adder;
82 this.phaser = phaser;
83 this.incs = incs;
84 }
85
86 public void run() {
87 phaser.arriveAndAwaitAdvance();
88 phaser.arriveAndAwaitAdvance();
89 DoubleAdder a = adder;
90 for (int i = 0; i < incs; ++i)
91 a.add(1.0);
92 result = a.sum();
93 phaser.arrive();
94 }
95 }
96
97 static final class SyncTask implements Runnable {
98 final SynchronizedDoubleAdder adder;
99 final Phaser phaser;
100 final int incs;
101 volatile double result;
102 SyncTask(SynchronizedDoubleAdder adder, Phaser phaser, int incs) {
103 this.adder = adder;
104 this.phaser = phaser;
105 this.incs = incs;
106 }
107
108 public void run() {
109 phaser.arriveAndAwaitAdvance();
110 phaser.arriveAndAwaitAdvance();
111 SynchronizedDoubleAdder a = adder;
112 for (int i = 0; i < incs; ++i)
113 a.add(1.0);
114 result = a.sum();
115 phaser.arrive();
116 }
117 }
118
119 }