本文整理汇总了Java中java.util.concurrent.atomic.AtomicReferenceFieldUpdater.compareAndSet方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicReferenceFieldUpdater.compareAndSet方法的具体用法?Java AtomicReferenceFieldUpdater.compareAndSet怎么用?Java AtomicReferenceFieldUpdater.compareAndSet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.atomic.AtomicReferenceFieldUpdater
的用法示例。
在下文中一共展示了AtomicReferenceFieldUpdater.compareAndSet方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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));
}
示例2: 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;
}
}
}
示例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.
*/
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;
}
}
}
示例4: 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;
}
}
}
示例5: 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;
}
示例6: 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);
}
}
示例7: removeSequence
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
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;
}
示例8: testAtomicReferenceFieldUpdater
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
static private void testAtomicReferenceFieldUpdater() {
System.out.println("AtomicTest.testAtomicReferenceFieldUpdater:");
AtomicTest obj = new AtomicTest();
AtomicReferenceFieldUpdater<AtomicTest, Integer> updater = AtomicReferenceFieldUpdater.newUpdater(AtomicTest.class, Integer.class, "value");
System.out.println(updater.get(obj));
updater.set(obj, 5);
System.out.println(updater.get(obj));
updater.compareAndSet(obj, 4, -4);
System.out.println(updater.get(obj));
updater.compareAndSet(obj, 5, -5);
System.out.println(updater.get(obj));
}
示例9: replace
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
/**
* Atomically replace the {@link Disposable} in the field with the given new Disposable
* but do not dispose the old one.
*
* @param updater the target field updater
* @param holder the target instance holding the field
* @param newValue the new Disposable to push, null allowed
* @return true if the operation succeeded, false if the target field contained
* the common {@link #DISPOSED} instance and the given disposable is not null but is disposed.
*/
static <T> boolean replace(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)) {
return true;
}
}
}
示例10: setOnce
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
/**
* Atomically push the field to the given non-null {@link Disposable} and return true,
* or return false if the field is non-null.
* If the target field contains the common {@link #DISPOSED} instance, the supplied disposable
* is disposed. If the field contains other non-null {@link Disposable}, an {@link IllegalStateException}
* is signalled to the {@code errorCallback}.
*
* @param updater the target field updater
* @param holder the target instance holding the field
* @param newValue the new Disposable to push, not null
* @return true if the operation succeeded, false
*/
public static <T> boolean setOnce(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, Disposable newValue,
Consumer<RuntimeException> errorCallback) {
Objects.requireNonNull(newValue, "newValue is null");
if (!updater.compareAndSet(holder, null, newValue)) {
newValue.dispose();
if (updater.get(holder) != DISPOSED) {
errorCallback.accept(new IllegalStateException("Disposable already pushed"));
}
return false;
}
return true;
}
示例11: replace
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
/**
* Atomically replace the {@link Disposable} in the field with the given new Disposable
* but do not dispose the old one.
*
* @param updater the target field updater
* @param holder the target instance holding the field
* @param newValue the new Disposable to push, null allowed
* @return true if the operation succeeded, false if the target field contained
* the common {@link #DISPOSED} instance and the given disposable is not null but is disposed.
*/
public static <T> boolean replace(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)) {
return true;
}
}
}
示例12: trySet
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
/**
* Atomically try to push the given {@link Disposable} on the field if it is null or
* disposes it if the field contains {@link #DISPOSED}.
*
* @param updater the target field updater
* @param holder the target instance holding the field
* @param newValue the disposable to push
* @return true if successful, false otherwise
*/
public static <T> boolean trySet(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, Disposable newValue) {
if (!updater.compareAndSet(holder, null, newValue)) {
if (updater.get(holder) == DISPOSED) {
newValue.dispose();
}
return false;
}
return true;
}
示例13: addSequence
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
static <T> void addSequence(final T holder,
final AtomicReferenceFieldUpdater<T, Sequence[]> updater,
final Sequence sequence) {
Sequence[] updatedSequences;
Sequence[] currentSequences;
do {
currentSequences = updater.get(holder);
updatedSequences = copyOf(currentSequences, currentSequences.length + 1);
updatedSequences[currentSequences.length] = sequence;
}
while (!updater.compareAndSet(holder, currentSequences, updatedSequences));
}
示例14: removeSequence
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
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;
}
示例15: replace
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //导入方法依赖的package包/类
/**
* A generic utility to atomically replace a subscription or cancel the replacement
* if the current subscription is marked as already cancelled (as in
* {@link #cancelledSubscription()}).
*
* @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 replace(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)) {
return true;
}
}
}