下面列出了怎么用java.util.concurrent.atomic.AtomicLongFieldUpdater的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Acquire a lock that allows reentry and attempts to acquire the lock while it is
* held can be detected by {@link #releaseReentrantLock(AtomicLongFieldUpdater, long, Object)}.
* <p>
* This lock <strong>must</strong> eventually be released by the same thread that acquired the lock. If the thread
* that acquires this lock is terminated before releasing the lock state is undefined.
* @param lockUpdater The {@link AtomicLongFieldUpdater} used to control the lock state.
* @param owner The owner of the lock object.
* @param <T> The type of object that owns the lock.
* @return {@code 0} if the acquire was unsuccessful, otherwise an identifier that must be passed to a subsequent
* call of {@link #releaseReentrantLock(AtomicLongFieldUpdater, long, Object)}.
*/
public static <T> long tryAcquireReentrantLock(final AtomicLongFieldUpdater<T> lockUpdater, final T owner) {
final long threadId = Thread.currentThread().getId();
for (;;) {
final long prevThreadId = lockUpdater.get(owner);
if (prevThreadId == REENTRANT_LOCK_ZERO_THREAD_ID) {
if (lockUpdater.compareAndSet(owner, REENTRANT_LOCK_ZERO_THREAD_ID, threadId)) {
return threadId;
}
} else if (prevThreadId == threadId || prevThreadId == -threadId) {
return -threadId;
} else if (lockUpdater.compareAndSet(owner, prevThreadId,
prevThreadId > REENTRANT_LOCK_ZERO_THREAD_ID ? -prevThreadId : prevThreadId)) {
return REENTRANT_LOCK_ZERO_THREAD_ID;
}
}
}
@SuppressWarnings("unchecked")
public static long request(long n, Object instance, AtomicLongFieldUpdater updater) {
for (;;) {
long currentDemand = updater.get(instance);
if (currentDemand == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long adjustedDemand = addCap(currentDemand, n);
if (updater.compareAndSet(instance, currentDemand, adjustedDemand)) {
return currentDemand;
}
}
}
@SuppressWarnings("unchecked")
public static long request(long n, Object instance, AtomicLongFieldUpdater updater) {
for (;;) {
long currentDemand = updater.get(instance);
if (currentDemand == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long adjustedDemand = addCap(currentDemand, n);
if (updater.compareAndSet(instance, currentDemand, adjustedDemand)) {
return currentDemand;
}
}
}
/**
* Try to increment/decrement the entrant count for base or a cell
*
* @return {@link UpdateResult} possibly indicating that the counter was updated, otherwise it
* experienced contention with another thread or the epoch was already closed
*/
private static <T> UpdateResult tryUpdate(final AtomicLongFieldUpdater<T> updater, final T obj,
final int delta) {
assert (delta & TERMINATED) == 0 : "increment/decrement shouldn't set terminate bit";
final long curr = updater.get(obj);
if ((curr & TERMINATED) != 0) {
return UpdateResult.CLOSED;
}
final long newVal = curr + delta;
if ((newVal & TERMINATED) != 0) {
// would likely take centuries to happen?
throw new IllegalStateException("excessively used epoch");
}
if (updater.compareAndSet(obj, curr, curr + delta)) {
return UpdateResult.SUCCESS;
} else {
return UpdateResult.CONFLICT;
}
}
/**
* Concurrent addition bound to Long.MAX_VALUE.
* Any concurrent write will "happen before" this operation.
*
* @param <T> the parent instance type
* @param updater current field updater
* @param instance current instance to update
* @param toAdd delta to add
* @return value before addition or Long.MAX_VALUE
*/
private static <T> long addCap(AtomicLongFieldUpdater<T> updater, T instance, long toAdd) {
long r, u;
for (;;) {
r = updater.get(instance);
if (r == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
u = r + toAdd;
if (u < 0L) {
u = Long.MAX_VALUE;
}
if (updater.compareAndSet(instance, r, u)) {
return r;
}
}
}
/**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws Exception {
x = 1;
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
Thread.yield();
}});
t.start();
assertTrue(a.compareAndSet(this, 1, 2));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertEquals(3, a.get(this));
}
static <T> long producedCancellable(AtomicLongFieldUpdater<T> updater, T instance, long n) {
for (; ; ) {
long current = updater.get(instance);
if (current == Long.MIN_VALUE) {
return Long.MIN_VALUE;
}
if (current == Long.MAX_VALUE) {
return Long.MAX_VALUE;
}
long update = current - n;
if (update < 0L) {
reportBadRequest(update);
update = 0L;
}
if (updater.compareAndSet(instance, current, update)) {
return update;
}
}
}
/**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws Exception {
x = 1;
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3))
Thread.yield();
}});
t.start();
assertTrue(a.compareAndSet(this, 1, 2));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertEquals(3, a.get(this));
}
/**
* Terminates the counter, forbidding new entrants from entering
*
* @return the number of entrants in the counter (possibly negative)
*/
private static <T> long terminateCounter(final AtomicLongFieldUpdater<T> updater, final T obj) {
// set the terminate bit (adding x + 1 is the same as x | 1 for even numbers)
final long oldVal = updater.getAndAdd(obj, 1);
if ((oldVal & TERMINATED) != 0) {
throw new IllegalStateException("counter already terminated (should be impossible)");
}
return StripedEpoch.decode(oldVal);
}
public static void main(String[] args) {
Person person = new Person("zhangsan", 11, 170);
person.setHobby(new Hobby("打球", "足球,篮球"));
AtomicIntegerFieldUpdater<Person> atomicIntegerFieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
atomicIntegerFieldUpdater.addAndGet(person, 12);
AtomicLongFieldUpdater<Person> atomicLongFieldUpdater = AtomicLongFieldUpdater.newUpdater(Person.class, "height");
atomicLongFieldUpdater.addAndGet(person, 180);
AtomicReferenceFieldUpdater<Person, Hobby> atomicReferenceFieldUpdater = AtomicReferenceFieldUpdater.newUpdater(Person.class, Hobby.class, "hobby");
atomicReferenceFieldUpdater.getAndSet(person, new Hobby("打球", "排球,羽毛球"));
}
public void checkPrivateAccess() {
try {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
AtomicLongFieldUpdater.newUpdater
(AtomicLongFieldUpdaterTest.class, "privateField");
shouldThrow();
} catch (RuntimeException success) {
assertNotNull(success.getCause());
}
}
public void checkCompareAndSetProtectedSub() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
AtomicLongFieldUpdater.newUpdater
(AtomicLongFieldUpdaterTest.class, "protectedField");
this.protectedField = 1;
assertTrue(a.compareAndSet(this, 1, 2));
assertTrue(a.compareAndSet(this, 2, -4));
assertEquals(-4, a.get(this));
assertFalse(a.compareAndSet(this, -5, 7));
assertEquals(-4, a.get(this));
assertTrue(a.compareAndSet(this, -4, 7));
assertEquals(7, a.get(this));
}
public void checkPackageAccess(AtomicLongFieldUpdaterTest obj) {
obj.x = 72L;
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
AtomicLongFieldUpdater.newUpdater
(AtomicLongFieldUpdaterTest.class, "x");
assertEquals(72L, a.get(obj));
assertTrue(a.compareAndSet(obj, 72L, 73L));
assertEquals(73L, a.get(obj));
}
public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) {
try {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
AtomicLongFieldUpdater.newUpdater
(AtomicLongFieldUpdaterTest.class, "privateField");
throw new AssertionError("should throw");
} catch (RuntimeException success) {
assertNotNull(success.getCause());
}
}
/**
* get returns the last value set or assigned
*/
public void testGetSet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(1, a.get(this));
a.set(this, 2);
assertEquals(2, a.get(this));
a.set(this, -3);
assertEquals(-3, a.get(this));
}
/**
* get returns the last value lazySet by same thread
*/
public void testGetLazySet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(1, a.get(this));
a.lazySet(this, 2);
assertEquals(2, a.get(this));
a.lazySet(this, -3);
assertEquals(-3, a.get(this));
}
/**
* compareAndSet succeeds in changing value if equal to expected else fails
*/
public void testCompareAndSet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertTrue(a.compareAndSet(this, 1, 2));
assertTrue(a.compareAndSet(this, 2, -4));
assertEquals(-4, a.get(this));
assertFalse(a.compareAndSet(this, -5, 7));
assertEquals(-4, a.get(this));
assertTrue(a.compareAndSet(this, -4, 7));
assertEquals(7, a.get(this));
}
/**
* compareAndSet succeeds in changing protected field value if
* equal to expected else fails
*/
public void testCompareAndSetProtected() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("protectedField");
protectedField = 1;
assertTrue(a.compareAndSet(this, 1, 2));
assertTrue(a.compareAndSet(this, 2, -4));
assertEquals(-4, a.get(this));
assertFalse(a.compareAndSet(this, -5, 7));
assertEquals(-4, a.get(this));
assertTrue(a.compareAndSet(this, -4, 7));
assertEquals(7, a.get(this));
}
/**
* repeated weakCompareAndSet succeeds in changing value when equal
* to expected
*/
public void testWeakCompareAndSet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
do {} while (!a.weakCompareAndSet(this, 1, 2));
do {} while (!a.weakCompareAndSet(this, 2, -4));
assertEquals(-4, a.get(this));
do {} while (!a.weakCompareAndSet(this, -4, 7));
assertEquals(7, a.get(this));
}
/**
* getAndSet returns previous value and sets to given value
*/
public void testGetAndSet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(1, a.getAndSet(this, 0));
assertEquals(0, a.getAndSet(this, -10));
assertEquals(-10, a.getAndSet(this, 1));
}
/**
* getAndAdd returns previous value and adds given value
*/
public void testGetAndAdd() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(1, a.getAndAdd(this, 2));
assertEquals(3, a.get(this));
assertEquals(3, a.getAndAdd(this, -4));
assertEquals(-1, a.get(this));
}
/**
* getAndDecrement returns previous value and decrements
*/
public void testGetAndDecrement() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(1, a.getAndDecrement(this));
assertEquals(0, a.getAndDecrement(this));
assertEquals(-1, a.getAndDecrement(this));
}
/**
* getAndIncrement returns previous value and increments
*/
public void testGetAndIncrement() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(1, a.getAndIncrement(this));
assertEquals(2, a.get(this));
a.set(this, -2);
assertEquals(-2, a.getAndIncrement(this));
assertEquals(-1, a.getAndIncrement(this));
assertEquals(0, a.getAndIncrement(this));
assertEquals(1, a.get(this));
}
/**
* addAndGet adds given value to current, and returns current value
*/
public void testAddAndGet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(3, a.addAndGet(this, 2));
assertEquals(3, a.get(this));
assertEquals(-1, a.addAndGet(this, -4));
assertEquals(-1, a.get(this));
}
/**
* decrementAndGet decrements and returns current value
*/
public void testDecrementAndGet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(0, a.decrementAndGet(this));
assertEquals(-1, a.decrementAndGet(this));
assertEquals(-2, a.decrementAndGet(this));
assertEquals(-2, a.get(this));
}
/**
* incrementAndGet increments and returns current value
*/
public void testIncrementAndGet() {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a;
a = updaterFor("x");
x = 1;
assertEquals(2, a.incrementAndGet(this));
assertEquals(2, a.get(this));
a.set(this, -2);
assertEquals(-1, a.incrementAndGet(this));
assertEquals(0, a.incrementAndGet(this));
assertEquals(1, a.incrementAndGet(this));
assertEquals(1, a.get(this));
}
/**
* AtomicLongFieldUpdater getAndUpdate returns previous value and updates
* result of supplied function
*/
public void testLongFieldUpdaterGetAndUpdate() {
AtomicLongFieldUpdater a = aLongFieldUpdater();
a.set(this, 1);
assertEquals(1L, a.getAndUpdate(this, Atomic8Test::addLong17));
assertEquals(18L, a.getAndUpdate(this, Atomic8Test::addLong17));
assertEquals(35L, a.get(this));
assertEquals(35L, aLongField);
}
/**
* AtomicLongFieldUpdater updateAndGet updates with supplied function and
* returns result.
*/
public void testLongFieldUpdaterUpdateAndGet() {
AtomicLongFieldUpdater a = aLongFieldUpdater();
a.set(this, 1);
assertEquals(18L, a.updateAndGet(this, Atomic8Test::addLong17));
assertEquals(35L, a.updateAndGet(this, Atomic8Test::addLong17));
assertEquals(35L, a.get(this));
assertEquals(35L, aLongField);
}
/**
* AtomicLongFieldUpdater getAndAccumulate returns previous value
* and updates with supplied function.
*/
public void testLongFieldUpdaterGetAndAccumulate() {
AtomicLongFieldUpdater a = aLongFieldUpdater();
a.set(this, 1);
assertEquals(1L, a.getAndAccumulate(this, 2L, Long::sum));
assertEquals(3L, a.getAndAccumulate(this, 3L, Long::sum));
assertEquals(6L, a.get(this));
assertEquals(6L, aLongField);
}
/**
* AtomicLongFieldUpdater accumulateAndGet updates with supplied
* function and returns result.
*/
public void testLongFieldUpdaterAccumulateAndGet() {
AtomicLongFieldUpdater a = aLongFieldUpdater();
a.set(this, 1);
assertEquals(7L, a.accumulateAndGet(this, 6L, Long::sum));
assertEquals(10L, a.accumulateAndGet(this, 3L, Long::sum));
assertEquals(10L, a.get(this));
assertEquals(10L, aLongField);
}