7 |
|
import java.util.concurrent.*; |
8 |
|
import java.util.concurrent.atomic.*; |
9 |
|
|
10 |
< |
/* |
10 |
> |
/** |
11 |
|
* A simple test for evaluating different implementations of |
12 |
|
* CopyOnWriteArrayList.addIfAbsent. |
13 |
|
*/ |
14 |
– |
|
14 |
|
public class COWALAddIfAbsentLoops { |
15 |
|
|
16 |
< |
static final int SIZE = 25000; |
16 |
> |
static final int SIZE = 35000; |
17 |
> |
|
18 |
> |
/** |
19 |
> |
* Set to 1 for 0% cache hit ratio (every addIfAbsent a cache miss). |
20 |
> |
* Set to 2 for 50% cache hit ratio. |
21 |
> |
*/ |
22 |
> |
static final int CACHE_HIT_FACTOR = 1; |
23 |
|
|
24 |
|
public static void main(String[] args) throws Exception { |
25 |
|
for (int reps = 0; reps < 4; ++reps) { |
28 |
|
} |
29 |
|
} |
30 |
|
|
31 |
< |
static AtomicInteger result = new AtomicInteger(); |
31 |
> |
static final AtomicInteger result = new AtomicInteger(); |
32 |
|
|
33 |
|
public static void test(int n) throws Exception { |
34 |
|
result.set(0); |
35 |
< |
Phaser started = new Phaser(n + 1); |
35 |
> |
Thread[] ts = new Thread[CACHE_HIT_FACTOR*n]; |
36 |
> |
Phaser started = new Phaser(ts.length + 1); |
37 |
|
CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<Integer>(); |
38 |
< |
Thread[] ts = new Thread[n]; |
39 |
< |
for (int i = 0; i < n; ++i) |
34 |
< |
(ts[i] = new Thread(new Task(i, n, list, started))).start(); |
38 |
> |
for (int i = 0; i < ts.length; ++i) |
39 |
> |
(ts[i] = new Thread(new Task(i%n, n, list, started))).start(); |
40 |
|
long p = started.arriveAndAwaitAdvance(); |
41 |
|
long st = System.nanoTime(); |
42 |
< |
for (int i = 0; i < n; ++i) |
43 |
< |
ts[i].join(); |
42 |
> |
for (Thread thread : ts) |
43 |
> |
thread.join(); |
44 |
|
double secs = ((double)System.nanoTime() - st) / (1000L * 1000 * 1000); |
45 |
|
System.out.println("Threads: " + n + " Time: " + secs); |
46 |
|
if (result.get() != SIZE) |