本文整理汇总了Java中java.util.concurrent.atomic.AtomicMarkableReference.compareAndSet方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicMarkableReference.compareAndSet方法的具体用法?Java AtomicMarkableReference.compareAndSet怎么用?Java AtomicMarkableReference.compareAndSet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.atomic.AtomicMarkableReference
的用法示例。
在下文中一共展示了AtomicMarkableReference.compareAndSet方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCompareAndSetInMultipleThreads
import java.util.concurrent.atomic.AtomicMarkableReference; //导入方法依赖的package包/类
/**
* compareAndSet in one thread enables another waiting for reference value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws Exception {
final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!ai.compareAndSet(two, three, false, false))
Thread.yield();
}});
t.start();
assertTrue(ai.compareAndSet(one, two, false, false));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertSame(three, ai.getReference());
assertFalse(ai.isMarked());
}
示例2: testCompareAndSetInMultipleThreads2
import java.util.concurrent.atomic.AtomicMarkableReference; //导入方法依赖的package包/类
/**
* compareAndSet in one thread enables another waiting for mark value
* to succeed
*/
public void testCompareAndSetInMultipleThreads2() throws Exception {
final AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!ai.compareAndSet(one, one, true, false))
Thread.yield();
}});
t.start();
assertTrue(ai.compareAndSet(one, one, false, true));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertSame(one, ai.getReference());
assertFalse(ai.isMarked());
}
示例3: add
import java.util.concurrent.atomic.AtomicMarkableReference; //导入方法依赖的package包/类
@Override
public boolean add(T value) {
Node newNode = new Node(value, null);
while (true) {
Pair<AtomicMarkableReference<Node>, Node> pair = find(value);
AtomicMarkableReference<Node> curr = pair.getKey();
Node next = pair.getValue();
if (next != null && next.value.equals(value) && !next.next.isMarked()) {
return false;
}
newNode.next.set(next, false);
if (curr.compareAndSet(next, newNode, false, false))
return true;
}
}
示例4: remove
import java.util.concurrent.atomic.AtomicMarkableReference; //导入方法依赖的package包/类
@Override
public boolean remove(T value) {
while (true) {
Pair<AtomicMarkableReference<Node>, Node> pair = find(value);
AtomicMarkableReference<Node> prev = pair.getKey();
Node curr = pair.getValue();
if (curr == null || !curr.value.equals(value))
return false;
Node next = curr.next.getReference();
if (!curr.next.compareAndSet(next, next, false, true))
continue;
prev.compareAndSet(curr, next, false, false);
return true;
}
}
示例5: find
import java.util.concurrent.atomic.AtomicMarkableReference; //导入方法依赖的package包/类
private Pair<AtomicMarkableReference<Node>, Node> find(T value) {
while (true) {
AtomicMarkableReference<Node> curr = head;
Node next = head.getReference();
while (true) {
if (next == null)
return new Pair<>(curr, null);
if (next.next.isMarked()) {
if (!curr.compareAndSet(next, next.next.getReference(), false, false))
break;
next = next.next.getReference();
} else {
if (curr.getReference().value.compareTo(value) >= 0)
return new Pair<>(curr, next);
curr = next.next;
next = curr.getReference();
}
}
}
}