4 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
< |
import junit.framework.*; |
8 |
< |
import java.util.concurrent.*; |
7 |
> |
import java.util.concurrent.Executors; |
8 |
> |
import java.util.concurrent.ExecutorService; |
9 |
> |
import java.util.concurrent.Phaser; |
10 |
|
import java.util.concurrent.atomic.LongAccumulator; |
11 |
|
|
12 |
+ |
import junit.framework.Test; |
13 |
+ |
import junit.framework.TestSuite; |
14 |
+ |
|
15 |
|
public class LongAccumulatorTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run(suite()); |
17 |
> |
main(suite(), args); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(LongAccumulatorTest.class); |
123 |
|
LongAccumulator a = new LongAccumulator(Long::max, 0L); |
124 |
|
Phaser phaser = new Phaser(nthreads + 1); |
125 |
|
for (int i = 0; i < nthreads; ++i) |
126 |
< |
pool.execute(new AccumulateerTask(a, phaser, incs)); |
126 |
> |
pool.execute(new AccTask(a, phaser, incs)); |
127 |
|
phaser.arriveAndAwaitAdvance(); |
128 |
|
phaser.arriveAndAwaitAdvance(); |
129 |
|
long expected = incs - 1; |
132 |
|
pool.shutdown(); |
133 |
|
} |
134 |
|
|
135 |
< |
static final class AccumulateerTask implements Runnable { |
136 |
< |
final LongAccumulator accumulateer; |
135 |
> |
static final class AccTask implements Runnable { |
136 |
> |
final LongAccumulator acc; |
137 |
|
final Phaser phaser; |
138 |
|
final int incs; |
139 |
|
volatile long result; |
140 |
< |
AccumulateerTask(LongAccumulator accumulateer, Phaser phaser, int incs) { |
141 |
< |
this.accumulateer = accumulateer; |
140 |
> |
AccTask(LongAccumulator acc, Phaser phaser, int incs) { |
141 |
> |
this.acc = acc; |
142 |
|
this.phaser = phaser; |
143 |
|
this.incs = incs; |
144 |
|
} |
145 |
|
|
146 |
|
public void run() { |
147 |
|
phaser.arriveAndAwaitAdvance(); |
148 |
< |
LongAccumulator a = accumulateer; |
148 |
> |
LongAccumulator a = acc; |
149 |
|
for (int i = 0; i < incs; ++i) |
150 |
|
a.accumulate(i); |
151 |
|
result = a.get(); |