/
ConcurrentArrayList.java
186 lines (143 loc) · 3.93 KB
/
ConcurrentArrayList.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
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.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Objects;
import java.util.RandomAccess;
/**
* only support add get set, not support remove
*
* @author Hank
*
* @param <T>
*/
public class ConcurrentArrayList<T> implements RandomAccess {
public static final int MAXIMUM_CAPACITY = 1 << 30;
private static final long SIZE_OFFSET;
private static final int ABASE;
private static final int ASHIFT;
private volatile Object[] values;
private volatile int size;// unsafe atomic operate
public ConcurrentArrayList() {
this(16);
}
public ConcurrentArrayList(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);
}
public void add(T value) {
final int index = insertIndex();
final long offset = offset(ABASE, ASHIFT, index);
for (;;) {// like cas
final Object[] before = values;
unsafe().putOrderedObject(before, offset, value);
final Object[] after = values;
if (before == after) {
return;
}
}
}
public void addAll(Collection<T> collection) {
if (collection == null) {
return;
}
ensureCapacity(size + collection.size());
for (T t : collection) {
add(t);
}
}
@SuppressWarnings("unchecked")
public T get(int index) {
Objects.checkIndex(index, size);
return (T) unsafe().getObjectVolatile(values, offset(ABASE, ASHIFT, index));
}
public void set(int index, T value) {
Objects.checkIndex(index, size);
final long offset = offset(ABASE, ASHIFT, index);
for (;;) {// like cas
final Object[] before = values;
unsafe().putOrderedObject(before, offset, value);
final Object[] after = values;
if (before == after) {
return;
}
}
}
public int size() {
return size;
}
public void clear() {
size = 0;
}
public ArrayList<T> toArrayList() {
int finalSize = size;
ArrayList<T> list = new ArrayList<>(finalSize);
for (int i = 0; i < finalSize; i++) {
list.add(get(i));
}
return list;
}
private int insertIndex() {
int index = unsafe().getAndAddInt(this, SIZE_OFFSET, 1);
ensureCapacity(index + 1);
return index;
}
private void ensureCapacity(int capacity) {
Object[] theArray = values;
if (theArray != null && theArray.length >= capacity) {
return;
}
synchronized (this) {
Object[] finalArray = values;
if (finalArray != null && finalArray.length >= capacity) {
return;
}
int newCapacity = tableSizeFor(capacity);
if (newCapacity > MAXIMUM_CAPACITY) {
throw new IndexOutOfBoundsException(newCapacity);
}
Object[] objs = new Object[newCapacity];
if (finalArray != null) {
System.arraycopy(finalArray, 0, objs, 0, finalArray.length);
}
values = objs;
}
}
static {
try {
Field field = ConcurrentArrayList.class.getDeclaredField("size");
SIZE_OFFSET = unsafe().objectFieldOffset(field);
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 (Throwable e) {
throw new Error(e);
}
}
public static void main(String[] args) {
ConcurrentArrayList<Integer> list = new ConcurrentArrayList<>();
for (int i = 0; i < 1024; i++) {
list.add(i);
}
for (int i = 0; i < 1024; i++) {
System.out.println(i + ":" + list.get(i));
}
for (int i = 0; i < 1024; i++) {
list.set(i, i + 10000);
}
for (int i = 0; i < 1024; i++) {
System.out.println(i + ":" + list.get(i));
}
}
}