下面列出了java.util.concurrent.atomic.AtomicLongArray#lazySet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public boolean offer(long value) {
final AtomicLongArray a = buffer;
final int m = mask;
final long pi = a.get(0);
int offset = calcOffset(pi, m);
int offset1 = offset + 1;
if (a.get(offset1) != 0L) {
return false;
}
a.lazySet(offset, value);
a.lazySet(offset1, 1L);
a.lazySet(0, pi + 2);
return true;
}
public long poll(boolean[] hasValue) {
final AtomicLongArray a = buffer;
final int m = mask;
final long ci = a.get(1);
int offset = calcOffset(ci, m);
int offset1 = offset + 1;
long v = a.get(offset1);
if (v == 0L) {
hasValue[0] = false;
return 0L;
}
hasValue[0] = true;
v = a.get(offset);
a.lazySet(offset1, 0L);
a.lazySet(1, ci + 2);
return v;
}
public long poll() {
final AtomicLongArray a = buffer;
final int m = mask;
final long ci = a.get(1);
int offset = calcOffset(ci, m);
long v = a.get(offset + 1);
if (v == 0L) {
return 0L;
}
v = a.get(offset);
a.lazySet(offset + 1, 0L);
a.lazySet(1, ci + 2);
return v;
}
/**
* get returns the last value lazySet at index by same thread
*/
public void testGetLazySet() {
AtomicLongArray aa = new AtomicLongArray(SIZE);
for (int i = 0; i < SIZE; i++) {
aa.lazySet(i, 1);
assertEquals(1, aa.get(i));
aa.lazySet(i, 2);
assertEquals(2, aa.get(i));
aa.lazySet(i, -3);
assertEquals(-3, aa.get(i));
}
}
/**
* Create an AtomicLongArray of the specified size, each value in the array will be EMPTY_BUCKET_VALUE
*/
private AtomicLongArray emptyKeyArray(int size) {
AtomicLongArray arr = new AtomicLongArray(size);
// Volatile store not required, could use plain store
// See VarHandles for JDK >= 9
for (int i = 0; i < arr.length(); i++) {
arr.lazySet(i, EMPTY_BUCKET_VALUE);
}
return arr;
}
/**
* get returns the last value lazySet at index by same thread
*/
public void testGetLazySet() {
AtomicLongArray aa = new AtomicLongArray(SIZE);
for (int i = 0; i < SIZE; i++) {
aa.lazySet(i, 1);
assertEquals(1, aa.get(i));
aa.lazySet(i, 2);
assertEquals(2, aa.get(i));
aa.lazySet(i, -3);
assertEquals(-3, aa.get(i));
}
}
static void spLongElement(AtomicLongArray buffer, int offset, long e)
{
buffer.lazySet(offset, e);
}
static void soLongElement(AtomicLongArray buffer, int offset, long e)
{
buffer.lazySet(offset, e);
}
protected final void soSequence(AtomicLongArray buffer, int offset, long e)
{
buffer.lazySet(offset, e);
}
protected static void soValue(AtomicLongArray elements, int index, long value)
{
elements.lazySet(calcValueOffset(index), value);
}