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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.yolean.concurrent;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* <p>This is a thread hash map for small collections that are stable once built. Until it is stable there will be a
* race among all threads missing something in the map. They will then clone the map add the missing stuff and then put
* it back as active again. Here are no locks, but the cost is that inserts will happen a lot more than necessary. The
* map reference is volatile, but on most multi-cpu machines that has no cost unless modified.</p>
*
* @author baldersheim
* @since 5.2
*/
public class CopyOnWriteHashMap<K, V> implements Map<K, V> {
private volatile HashMap<K, V> map = new HashMap<>();
@Override
public int size() {
return map.size();
}
@Override
public boolean isEmpty() {
return map.isEmpty();
}
@Override
public boolean containsKey(Object key) {
return map.containsKey(key);
}
@Override
public boolean containsValue(Object value) {
return map.containsValue(value);
}
@Override
public V get(Object key) {
return map.get(key);
}
@Override
public V put(K key, V value) {
HashMap<K, V> next = new HashMap<>(map);
V old = next.put(key, value);
map = next;
return old;
}
@Override
@SuppressWarnings("SuspiciousMethodCalls")
public V remove(Object key) {
HashMap<K, V> prev = map;
if (!prev.containsKey(key)) {
return null;
}
HashMap<K, V> next = new HashMap<>(prev);
V old = next.remove(key);
map = next;
return old;
}
@Override
public void putAll(Map<? extends K, ? extends V> m) {
HashMap<K, V> next = new HashMap<>(map);
next.putAll(m);
map = next;
}
@Override
public void clear() {
map = new HashMap<>();
}
@Override
public Set<K> keySet() {
return map.keySet();
}
@Override
public Collection<V> values() {
return map.values();
}
@Override
public Set<Entry<K, V>> entrySet() {
return map.entrySet();
}
}
|