本文整理汇总了Java中java.util.concurrent.atomic.AtomicLongFieldUpdater类的典型用法代码示例。如果您正苦于以下问题:Java AtomicLongFieldUpdater类的具体用法?Java AtomicLongFieldUpdater怎么用?Java AtomicLongFieldUpdater使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AtomicLongFieldUpdater类属于java.util.concurrent.atomic包,在下文中一共展示了AtomicLongFieldUpdater类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCompareAndSetInMultipleThreads
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例2: producedCancellable
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
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;
}
}
}
示例3: getAndAddRequest
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
public static <T> long getAndAddRequest(AtomicLongFieldUpdater<T> requested, T object, long n) {
long current;
do {
current = requested.get(object);
} while (!requested.compareAndSet(object, current, addCap(current, n)));
return current;
}
示例4: checkPrivateAccess
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
public void checkPrivateAccess() {
try {
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
AtomicLongFieldUpdater.newUpdater
(AtomicLongFieldUpdaterTest.class, "privateField");
shouldThrow();
} catch (RuntimeException success) {
assertNotNull(success.getCause());
}
}
示例5: checkCompareAndSetProtectedSub
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
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));
}
示例6: checkPackageAccess
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
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));
}
示例7: testGetSet
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例8: testGetLazySet
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例9: testCompareAndSet
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例10: testCompareAndSetProtected
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例11: testWeakCompareAndSet
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例12: testGetAndSet
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例13: testGetAndAdd
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例14: testGetAndDecrement
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}
示例15: testGetAndIncrement
import java.util.concurrent.atomic.AtomicLongFieldUpdater; //导入依赖的package包/类
/**
* 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));
}