Код: Выделить всё
public class ConcurrentArrayStack implements Stack {
private final AtomicInteger size;
private final T array[];
public ConcurrentArrayStack(int maxCapacity) {
this.array = (T[]) new Object[maxCapacity];
this.size = new AtomicInteger(0);
}
@Override
public T push(T t) {
if(t == null) {
throw new NullPointerException("Null values are not allowed");
}
int currentSize;
T currentValue;
do {
currentSize = size.get();
if(currentSize == array.length) {
return null;
}
currentValue = array[currentSize];
} while(currentValue != null || !size.compareAndSet(currentSize, currentSize + 1));
array[currentSize] = t;
VarHandle.fullFence(); // в конце каждого метода заключается Барьер компилятора (промыть любое ожидающее чтение/запись на уровне компилятора) Нам не нужен забор между нагрузкой от массива и хранением до массива, поскольку нагрузки не переупорядочиваются в более ранние магазины (x86). < /li>
< /ol>
[b] Вопрос: [/b] то, что я не мог проверить, что Fullfence Код: Выделить всё
releaseПодробнее здесь: https://stackoverflow.com/questions/794 ... -necessary