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 = Integer.getInteger("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 = Integer.getInteger("cache.hit.factor", 1); |
23 |
> |
|
24 |
> |
static final int MAX_STRIPES = Integer.getInteger("max.stripes", 3); |
25 |
> |
|
26 |
> |
static final int REPS = Integer.getInteger("reps", 3); |
27 |
|
|
28 |
|
public static void main(String[] args) throws Exception { |
29 |
< |
for (int reps = 0; reps < 4; ++reps) { |
30 |
< |
for (int i = 1; i <= 4; ++i) |
29 |
> |
for (int reps = 0; reps < REPS; ++reps) { |
30 |
> |
for (int i = 1; i <= MAX_STRIPES; ++i) |
31 |
|
test(i); |
32 |
|
} |
33 |
|
} |
34 |
|
|
35 |
< |
static AtomicInteger result = new AtomicInteger(); |
35 |
> |
static final AtomicInteger result = new AtomicInteger(); |
36 |
|
|
37 |
|
public static void test(int n) throws Exception { |
38 |
|
result.set(0); |
39 |
< |
Phaser started = new Phaser(n + 1); |
39 |
> |
Thread[] ts = new Thread[CACHE_HIT_FACTOR*n]; |
40 |
> |
Phaser started = new Phaser(ts.length + 1); |
41 |
|
CopyOnWriteArrayList<Integer> list = new CopyOnWriteArrayList<Integer>(); |
42 |
< |
Thread[] ts = new Thread[n]; |
43 |
< |
for (int i = 0; i < n; ++i) |
34 |
< |
(ts[i] = new Thread(new Task(i, n, list, started))).start(); |
42 |
> |
for (int i = 0; i < ts.length; ++i) |
43 |
> |
(ts[i] = new Thread(new Task(i%n, n, list, started))).start(); |
44 |
|
long p = started.arriveAndAwaitAdvance(); |
45 |
|
long st = System.nanoTime(); |
46 |
< |
for (int i = 0; i < n; ++i) |
47 |
< |
ts[i].join(); |
46 |
> |
for (Thread thread : ts) |
47 |
> |
thread.join(); |
48 |
|
double secs = ((double)System.nanoTime() - st) / (1000L * 1000 * 1000); |
49 |
|
System.out.println("Threads: " + n + " Time: " + secs); |
50 |
|
if (result.get() != SIZE) |