forked from stephenc/high-scale-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
NonBlockingHashMapLongZeroKeyConcurrentGetPutIfAbsentTest.java
102 lines (84 loc) · 3.22 KB
/
NonBlockingHashMapLongZeroKeyConcurrentGetPutIfAbsentTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package org.cliffc.high_scale_lib.non_blocking_hashmap;
import junit.framework.TestCase;
import org.cliffc.high_scale_lib.NonBlockingHashMapLong;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.*;
public class NonBlockingHashMapLongZeroKeyConcurrentGetPutIfAbsentTest extends TestCase {
private static final int N_THREADS = 4;
private static final int ITERATIONS = 1000000;
private ExecutorService service;
protected void setUp() {
service = Executors.newFixedThreadPool(N_THREADS);
}
protected void tearDown() throws Exception {
service.shutdownNow();
service.awaitTermination(1, TimeUnit.HOURS);
}
public void test_empty_map_key_0() throws Exception {
empty_map_test(0);
}
public void test_empty_map_key_123() throws Exception {
empty_map_test(123);
}
private void empty_map_test(long key) throws Exception {
for (int i = 0; i < ITERATIONS; i++) {
Set<String> results = runIteration(new NonBlockingHashMapLong<String>(), key);
assertEquals(results.toString(), 1, results.size());
}
}
private Set<String> runIteration(NonBlockingHashMapLong<String> map, long key) throws Exception {
List<Callable<String>> tasks = new ArrayList<Callable<String>>(N_THREADS);
for (int i = 1; i <= N_THREADS; i++) {
tasks.add(new PutIfAbsent(map, key, "worker #" + i));
}
List<String> results = executeTasks(tasks);
return new HashSet<String>(results);
}
private List<String> executeTasks(List<Callable<String>> tasks) throws Exception {
List<Future<String>> futures = new ArrayList<Future<String>>(N_THREADS);
final CountDownLatch startLatch = new CountDownLatch(N_THREADS + 1);
for (Callable<String> t : tasks) {
final Callable<String> task = t;
Future<String> future = service.submit(new Callable<String>() {
@Override
public String call() throws Exception {
startLatch.countDown();
startLatch.await();
return task.call();
}
});
futures.add(future);
}
startLatch.countDown();
List<String> results = new ArrayList<String>(N_THREADS);
for (Future<String> f : futures) {
results.add(f.get());
}
return results;
}
private static class PutIfAbsent implements Callable<String> {
private final NonBlockingHashMapLong<String> map;
private final String newValue;
private final long key;
public PutIfAbsent(NonBlockingHashMapLong<String> map, long key, String newValue) {
this.map = map;
this.newValue = newValue;
this.key = key;
}
@Override
public String call() throws Exception {
String value = map.get(key);
if (value == null) {
value = newValue;
String tmp = map.putIfAbsent(key, value);
if (tmp != null) {
return tmp;
}
}
return value;
}
}
}