ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/COWALAddIfAbsentStringLoops.java
Revision: 1.3
Committed: Wed Jun 12 18:14:41 2013 UTC (10 years, 10 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +2 -1 lines
Log Message:
un-diamond for maximal portability

File Contents

# User Rev Content
1 jsr166 1.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.*;
8     import java.util.concurrent.atomic.*;
9    
10     /**
11     * A simple test for evaluating different implementations of
12     * CopyOnWriteArrayList.addIfAbsent.
13     *
14     * This benchmark differs from COWALAddIfAbsentLoops mainly by having
15     * a more expensive element equals method.
16     */
17     public class COWALAddIfAbsentStringLoops {
18    
19     static final int SIZE = Integer.getInteger("size", 25000);
20    
21     /**
22     * Set to 1 for 0% cache hit ratio (every addIfAbsent a cache miss).
23     * Set to 2 for 50% cache hit ratio.
24     */
25     static final int CACHE_HIT_FACTOR = Integer.getInteger("cache.hit.factor", 1);
26    
27 jsr166 1.2 static final int MAX_STRIPES = Integer.getInteger("max.stripes", 4);
28 jsr166 1.1
29     static final int REPS = Integer.getInteger("reps", 3);
30    
31     public static void main(String[] args) throws Exception {
32     for (int reps = 0; reps < REPS; ++reps) {
33     for (int i = 1; i <= MAX_STRIPES; ++i)
34     test(i);
35     }
36     }
37    
38     static final AtomicInteger result = new AtomicInteger();
39    
40     public static void test(int n) throws Exception {
41     result.set(0);
42     Thread[] ts = new Thread[CACHE_HIT_FACTOR*n];
43     Phaser started = new Phaser(ts.length + 1);
44 jsr166 1.3 CopyOnWriteArrayList<String> list =
45     new CopyOnWriteArrayList<String>();
46 jsr166 1.1 for (int i = 0; i < ts.length; ++i)
47     (ts[i] = new Thread(new Task(i%n, n, list, started))).start();
48     long p = started.arriveAndAwaitAdvance();
49     long st = System.nanoTime();
50     for (Thread thread : ts)
51     thread.join();
52     double secs = ((double)System.nanoTime() - st) / (1000L * 1000 * 1000);
53     System.out.println("Threads: " + n + " Time: " + secs);
54     if (result.get() != SIZE)
55     throw new Error();
56     }
57    
58     static final class Task implements Runnable {
59     final int id, stride;
60     final CopyOnWriteArrayList<String> list;
61     final Phaser started;
62     Task(int id, int stride,
63     CopyOnWriteArrayList<String> list, Phaser started) {
64     this.id = id;
65     this.stride = stride;
66     this.list = list;
67     this.started = started;
68     }
69     public void run() {
70     final CopyOnWriteArrayList<String> list = this.list;
71     int origin = id, inc = stride, adds = 0;
72     started.arriveAndAwaitAdvance();
73     for (int i = origin; i < SIZE; i += inc) {
74     if (list.addIfAbsent("asjdklfjsdfjsdjf" + i))
75     ++adds;
76     }
77     result.getAndAdd(adds);
78     }
79     }
80     }