本文整理汇总了Java中java.util.concurrent.atomic.AtomicReferenceFieldUpdater类的典型用法代码示例。如果您正苦于以下问题:Java AtomicReferenceFieldUpdater类的具体用法?Java AtomicReferenceFieldUpdater怎么用?Java AtomicReferenceFieldUpdater使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AtomicReferenceFieldUpdater类属于java.util.concurrent.atomic包,在下文中一共展示了AtomicReferenceFieldUpdater类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testAPI
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* Basic tests of the API : Simple function calls to exercise all of the functions listed in the API
* specification for the AtomicReferenceFieldUpdater class.
*/
public void testAPI()
{
// =================================================================================
// Create instances of AtomicReferenceFieldUpdater to work with
for(int i = 0; i < updaters.length; i++)
{
updaters[i] = AtomicReferenceFieldUpdater.newUpdater(AtomicTestObject.class, String.class, "volatileString");
}
// =================================================================================
// Basic API tests
assertEquals("1 : get()", "the answer", getRandomUpdater().get(testObject1));
assertEquals("2 : get()", getRandomUpdater().get(testObject1), getRandomUpdater().get(testObject2));
assertEquals("3 : get()", getRandomUpdater().get(testObject1), getRandomUpdater().get(testObject1));
assertEquals("4 : get()", getRandomUpdater().get(testObject2), getRandomUpdater().get(testObject2));
assertEquals("5 : getAndSet()", "the answer", getRandomUpdater().getAndSet(testObject1, "the question"));
assertEquals("6 : get()", "the question", getRandomUpdater().get(testObject1));
assertEquals("7 : getAndSet()", "the question", getRandomUpdater().getAndSet(testObject1, "the answer"));
assertEquals("8 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject1, "the answer", "the question"));
assertEquals("9 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject2, "the answer", "the question"));
assertEquals("10: get()", getRandomUpdater().get(testObject1), getRandomUpdater().get(testObject2));
assertEquals("11: compareAndSet()", false, getRandomUpdater().compareAndSet(testObject1, "the answer", "the question"));
assertEquals("12: compareAndSet()", true, getRandomUpdater().compareAndSet(testObject1, "the question", "the answer"));
}
示例2: testCompareAndSetInMultipleThreads
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* compareAndSet in one thread enables another waiting for value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws Exception {
x = one;
final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
a = updaterFor("x");
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
Thread.yield();
}});
t.start();
assertTrue(a.compareAndSet(this, one, two));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertSame(three, a.get(this));
}
示例3: set
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* Atomically push the field to a {@link Disposable} and dispose the old content.
*
* @param updater the target field updater
* @param holder the target instance holding the field
* @param newValue the new Disposable to push
* @return true if successful, false if the field contains the {@link #DISPOSED} instance.
*/
static <T> boolean set(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, @Nullable Disposable newValue) {
for (;;) {
Disposable current = updater.get(holder);
if (current == DISPOSED) {
if (newValue != null) {
newValue.dispose();
}
return false;
}
if (updater.compareAndSet(holder, current, newValue)) {
if (current != null) {
current.dispose();
}
return true;
}
}
}
示例4: set
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* Atomically push the field to a {@link Disposable} and dispose the old content.
*
* @param updater the target field updater
* @param holder the target instance holding the field
* @param newValue the new Disposable to push
* @return true if successful, false if the field contains the {@link #DISPOSED} instance.
*/
public static <T> boolean set(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, @Nullable Disposable newValue) {
for (;;) {
Disposable current = updater.get(holder);
if (current == DISPOSED) {
if (newValue != null) {
newValue.dispose();
}
return false;
}
if (updater.compareAndSet(holder, current, newValue)) {
if (current != null) {
current.dispose();
}
return true;
}
}
}
示例5: set
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* A generic utility to atomically replace a subscription or cancel the replacement
* if current subscription is marked as cancelled (as in {@link #cancelledSubscription()})
* or was concurrently updated before.
* <p>
* The replaced subscription is itself cancelled.
*
* @param field The Atomic container
* @param instance the instance reference
* @param s the subscription
* @param <F> the instance type
*
* @return true if replaced
*/
public static <F> boolean set(AtomicReferenceFieldUpdater<F, Subscription> field,
F instance,
Subscription s) {
for (; ; ) {
Subscription a = field.get(instance);
if (a == CancelledSubscription.INSTANCE) {
s.cancel();
return false;
}
if (field.compareAndSet(instance, a, s)) {
if (a != null) {
a.cancel();
}
return true;
}
}
}
示例6: removeSequence
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* 原子移除某个指定的sequence
*
* @param holder 原子更新的域所属的类对象
* @param sequenceUpdater 原子更新的域对象
* @param sequence 要移除的sequence
* @param <T>
* @return
*/
public static <T> boolean removeSequence(
final T holder,
final AtomicReferenceFieldUpdater<T, Sequence[]> sequenceUpdater,
final Sequence sequence)
{
int numToRemove;
Sequence[] oldSequences;
Sequence[] newSequences;
do
{
oldSequences = sequenceUpdater.get(holder);
numToRemove = countMatching(oldSequences, sequence);
if (0 == numToRemove)
{
break;
}
final int oldSize = oldSequences.length;
newSequences = new Sequence[oldSize - numToRemove];
for (int i = 0, pos = 0; i < oldSize; i++)
{
final Sequence testSequence = oldSequences[i];
if (sequence != testSequence)
{
newSequences[pos++] = testSequence;
}
}
}
while (!sequenceUpdater.compareAndSet(holder, oldSequences, newSequences));
return numToRemove != 0;
}
示例7: SafeAtomicHelper
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
SafeAtomicHelper(
AtomicReferenceFieldUpdater<Waiter, Thread> waiterThreadUpdater,
AtomicReferenceFieldUpdater<Waiter, Waiter> waiterNextUpdater,
AtomicReferenceFieldUpdater<AbstractFuture, Waiter> waitersUpdater,
AtomicReferenceFieldUpdater<AbstractFuture, Listener> listenersUpdater,
AtomicReferenceFieldUpdater<AbstractFuture, Object> valueUpdater) {
this.waiterThreadUpdater = waiterThreadUpdater;
this.waiterNextUpdater = waiterNextUpdater;
this.waitersUpdater = waitersUpdater;
this.listenersUpdater = listenersUpdater;
this.valueUpdater = valueUpdater;
}
示例8: testReferenceFieldUpdaterGetAndUpdate
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* AtomicReferenceFieldUpdater getAndUpdate returns previous value
* and updates result of supplied function
*/
public void testReferenceFieldUpdaterGetAndUpdate() {
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
a.set(this, one);
assertEquals((Integer) 1, a.getAndUpdate(this, Atomic8Test::addInteger17));
assertEquals((Integer) 18, a.getAndUpdate(this, Atomic8Test::addInteger17));
assertEquals((Integer) 35, a.get(this));
assertEquals((Integer) 35, anIntegerField);
}
示例9: testReferenceFieldUpdaterUpdateAndGet
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* AtomicReferenceFieldUpdater updateAndGet updates with supplied
* function and returns result.
*/
public void testReferenceFieldUpdaterUpdateAndGet() {
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
a.set(this, one);
assertEquals((Integer) 18, a.updateAndGet(this, Atomic8Test::addInteger17));
assertEquals((Integer) 35, a.updateAndGet(this, Atomic8Test::addInteger17));
assertEquals((Integer) 35, a.get(this));
assertEquals((Integer) 35, anIntegerField);
}
示例10: testReferenceFieldUpdaterGetAndAccumulate
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* AtomicReferenceFieldUpdater returns previous value and updates
* with supplied function.
*/
public void testReferenceFieldUpdaterGetAndAccumulate() {
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
a.set(this, one);
assertEquals((Integer) 1, a.getAndAccumulate(this, 2, Atomic8Test::sumInteger));
assertEquals((Integer) 3, a.getAndAccumulate(this, 3, Atomic8Test::sumInteger));
assertEquals((Integer) 6, a.get(this));
assertEquals((Integer) 6, anIntegerField);
}
示例11: testReferenceFieldUpdaterAccumulateAndGet
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* AtomicReferenceFieldUpdater accumulateAndGet updates with
* supplied function and returns result.
*/
public void testReferenceFieldUpdaterAccumulateAndGet() {
AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
a.set(this, one);
assertEquals((Integer) 7, a.accumulateAndGet(this, 6, Atomic8Test::sumInteger));
assertEquals((Integer) 10, a.accumulateAndGet(this, 3, Atomic8Test::sumInteger));
assertEquals((Integer) 10, a.get(this));
assertEquals((Integer) 10, anIntegerField);
}
示例12: testFieldUpdaters_ClassCastException
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
/**
* Object arguments for parameters of type T that are not
* instances of the class passed to the newUpdater call will
* result in a ClassCastException being thrown.
*/
public void testFieldUpdaters_ClassCastException() {
// Use raw types to allow passing wrong object type, provoking CCE
final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater();
final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater();
final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater();
final Object obj = new Object();
for (Object x : new Object[]{ new Object(), null }) {
Runnable[] throwingActions = {
() -> longUpdater.get(x),
() -> intUpdater.get(x),
() -> refUpdater.get(x),
() -> longUpdater.set(x, 17L),
() -> intUpdater.set(x, 17),
() -> refUpdater.set(x, (Integer) 17),
() -> longUpdater.addAndGet(x, 17L),
() -> intUpdater.addAndGet(x, 17),
() -> longUpdater.getAndUpdate(x, y -> y),
() -> intUpdater.getAndUpdate(x, y -> y),
() -> refUpdater.getAndUpdate(x, y -> y),
() -> longUpdater.compareAndSet(x, 17L, 42L),
() -> intUpdater.compareAndSet(x, 17, 42),
() -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42),
};
assertThrows(ClassCastException.class, throwingActions);
}
}
示例13: checkPrivateAccess
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
public void checkPrivateAccess() {
try {
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
AtomicReferenceFieldUpdater.newUpdater
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
shouldThrow();
} catch (RuntimeException success) {
assertNotNull(success.getCause());
}
}
示例14: checkCompareAndSetProtectedSub
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
public void checkCompareAndSetProtectedSub() {
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
AtomicReferenceFieldUpdater.newUpdater
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField");
this.protectedField = one;
assertTrue(a.compareAndSet(this, one, two));
assertTrue(a.compareAndSet(this, two, m4));
assertSame(m4, a.get(this));
assertFalse(a.compareAndSet(this, m5, seven));
assertNotSame(seven, a.get(this));
assertTrue(a.compareAndSet(this, m4, seven));
assertSame(seven, a.get(this));
}
示例15: checkPackageAccess
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入依赖的package包/类
public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) {
obj.x = one;
AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
AtomicReferenceFieldUpdater.newUpdater
(AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
assertSame(one, a.get(obj));
assertTrue(a.compareAndSet(obj, one, two));
assertSame(two, a.get(obj));
}