-
Notifications
You must be signed in to change notification settings - Fork 119
/
ConcurrentIntToObjectArrayMap.java
253 lines (201 loc) · 5.01 KB
/
ConcurrentIntToObjectArrayMap.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
package rpc.turbo.util.concurrent;
import static rpc.turbo.util.TableUtils.offset;
import static rpc.turbo.util.TableUtils.tableSizeFor;
import static rpc.turbo.util.UnsafeUtils.unsafe;
import java.util.Arrays;
import java.util.function.Supplier;
/**
* 高性能,仅适用于key值比较少的情况,不能超过512k个
*
* @author Hank
*
*/
public class ConcurrentIntToObjectArrayMap<T> {
private static final Object NOT_FOUND = new Object();
private static final int MAXIMUM_CAPACITY = 1024 * 512;
private static final int ABASE;
private static final int ASHIFT;
private volatile Object[] array;
public ConcurrentIntToObjectArrayMap() {
this(16);
}
public ConcurrentIntToObjectArrayMap(int initialCapacity) {
if (initialCapacity < 2) {
throw new IllegalArgumentException("Illegal initial capacity: " + initialCapacity);
}
if (initialCapacity > MAXIMUM_CAPACITY) {
throw new IndexOutOfBoundsException("Illegal initial capacity: " + initialCapacity);
}
ensureCapacity(initialCapacity);
}
/**
*
* @param key
* @return
*/
public boolean contains(int key) {
if (key < 0) {
return false;
}
Object[] finalArray = array;
if (key >= finalArray.length) {
return false;
}
Object value = unsafe().getObjectVolatile(finalArray, offset(ABASE, ASHIFT, key));
if (value == NOT_FOUND) {
return false;
} else {
return true;
}
}
/**
*
* @param key
* @return null为找不到
*/
@SuppressWarnings("unchecked")
public T get(int key) {
if (key < 0) {
throw new IllegalArgumentException("Illegal key: " + key);
}
Object[] finalArray = array;
if (key >= finalArray.length) {
return null;
}
Object value = unsafe().getObjectVolatile(finalArray, offset(ABASE, ASHIFT, key));
if (value != NOT_FOUND) {
return (T) value;
} else {
return null;
}
}
@SuppressWarnings("unchecked")
public T getOrUpdate(int key, Supplier<T> producer) {
if (key < 0) {
throw new IllegalArgumentException("Illegal key: " + key);
}
Object value;
final Object[] finalArray = array;
if (key >= finalArray.length) {
value = NOT_FOUND;
} else {
value = unsafe().getObjectVolatile(finalArray, offset(ABASE, ASHIFT, key));
}
if (value != NOT_FOUND) {
return (T) value;
}
synchronized (this) {
final Object[] theArray = array;
if (key < theArray.length) {
value = unsafe().getObjectVolatile(theArray, offset(ABASE, ASHIFT, key));
}
if (value != NOT_FOUND) {
return (T) value;
}
value = producer.get();
put(key, (T) value);
return (T) value;
}
}
/**
*
* @param key
* 大于零,小于512k
*
* @param value
*/
public void put(int key, T value) {
if (key < 0) {
throw new IllegalArgumentException("Illegal key: " + key);
}
if (key >= MAXIMUM_CAPACITY) {
throw new IndexOutOfBoundsException("Illegal key: " + key);
}
ensureCapacity(key + 1);
final long offset = offset(ABASE, ASHIFT, key);
for (;;) {// like cas
final Object[] before = array;
unsafe().putOrderedObject(before, offset, value);
final Object[] after = array;
if (before == after) {
return;
}
}
}
/**
* 重置为未赋值
*
* @param key
* 大于零,小于512k
*/
public boolean remove(final int key) {
if (key < 0) {
return false;
}
Object[] finalArray = array;
if (key >= finalArray.length) {
return false;
}
final long offset = offset(ABASE, ASHIFT, key);
for (;;) {// like cas
final Object[] before = array;
unsafe().putOrderedObject(before, offset, NOT_FOUND);
final Object[] after = array;
if (before == after) {
return true;
}
}
}
public void clear() {
if (array == null) {
return;
}
Object[] objs = new Object[16];
Arrays.fill(objs, NOT_FOUND);
array = objs;
}
private void ensureCapacity(int capacity) {
Object[] theArray = array;
if (theArray != null && theArray.length >= capacity) {
return;
}
synchronized (this) {
Object[] finalArray = array;
if (finalArray != null && finalArray.length >= capacity) {
return;
}
int newCapacity = tableSizeFor(capacity);
if (newCapacity > MAXIMUM_CAPACITY) {
throw new IndexOutOfBoundsException(newCapacity);
}
Object[] objs = new Object[newCapacity];
Arrays.fill(objs, NOT_FOUND);
if (finalArray != null) {
System.arraycopy(finalArray, 0, objs, 0, finalArray.length);
}
array = objs;
}
}
static {
try {
ABASE = unsafe().arrayBaseOffset(Object[].class);
int scale = unsafe().arrayIndexScale(Object[].class);
if ((scale & (scale - 1)) != 0) {
throw new Error("array index scale not a power of two");
}
ASHIFT = 31 - Integer.numberOfLeadingZeros(scale);
} catch (Exception e) {
throw new Error(e);
}
}
public static void main(String[] args) {
ConcurrentIntToObjectArrayMap<Integer> map = new ConcurrentIntToObjectArrayMap<>();
map.put(16, 16);
for (int i = 0; i < 1024; i++) {
map.put(i, i);
}
for (int i = 0; i < 1024; i++) {
System.out.println(i + ":" + map.get(i));
}
}
}