本文整理汇总了Java中java.util.concurrent.atomic.AtomicMarkableReference类的典型用法代码示例。如果您正苦于以下问题:Java AtomicMarkableReference类的具体用法?Java AtomicMarkableReference怎么用?Java AtomicMarkableReference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AtomicMarkableReference类属于java.util.concurrent.atomic包,在下文中一共展示了AtomicMarkableReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetSet
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
/**
* get returns the last values of reference and mark set
*/
public void testGetSet() {
boolean[] mark = new boolean[1];
AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
assertSame(one, ai.getReference());
assertFalse(ai.isMarked());
assertSame(one, ai.get(mark));
assertFalse(mark[0]);
ai.set(two, false);
assertSame(two, ai.getReference());
assertFalse(ai.isMarked());
assertSame(two, ai.get(mark));
assertFalse(mark[0]);
ai.set(one, true);
assertSame(one, ai.getReference());
assertTrue(ai.isMarked());
assertSame(one, ai.get(mark));
assertTrue(mark[0]);
}
示例2: testCompareAndSet
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
/**
* compareAndSet succeeds in changing values if equal to expected reference
* and mark else fails
*/
public void testCompareAndSet() {
boolean[] mark = new boolean[1];
AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
assertSame(one, ai.get(mark));
assertFalse(ai.isMarked());
assertFalse(mark[0]);
assertTrue(ai.compareAndSet(one, two, false, false));
assertSame(two, ai.get(mark));
assertFalse(mark[0]);
assertTrue(ai.compareAndSet(two, m3, false, true));
assertSame(m3, ai.get(mark));
assertTrue(mark[0]);
assertFalse(ai.compareAndSet(two, m3, true, true));
assertSame(m3, ai.get(mark));
assertTrue(mark[0]);
}
示例3: 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());
}
示例4: 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());
}
示例5: testWeakCompareAndSet
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
/**
* repeated weakCompareAndSet succeeds in changing values when equal
* to expected
*/
public void testWeakCompareAndSet() {
boolean[] mark = new boolean[1];
AtomicMarkableReference ai = new AtomicMarkableReference(one, false);
assertSame(one, ai.get(mark));
assertFalse(ai.isMarked());
assertFalse(mark[0]);
do {} while (!ai.weakCompareAndSet(one, two, false, false));
assertSame(two, ai.get(mark));
assertFalse(mark[0]);
do {} while (!ai.weakCompareAndSet(two, m3, false, true));
assertSame(m3, ai.get(mark));
assertTrue(mark[0]);
}
示例6: add
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
@Override
public boolean add(T value) {
while (true) {
// System.out.print(value);
// System.out.println(" add");
NodePair nodePair = find(value); // find place for insert
Node parent = nodePair.parent, child = nodePair.child;
// if current is not a tail and it has the same value
if (child.item != null && child.get().compareTo(value) == 0) { // check for unique
return false;
}
// else add new node
Node new_node = new Node(value);
new_node.next = new AtomicMarkableReference<>(child, false);
// IF (|parent.next| -> |child|) AND (parent.marked = false)
// THEN (|parent.next| -> |new_node|) AND (new_node.marked = false)
if (parent.next.compareAndSet(child, new_node, false, false)) {
return true;
}
}
}
示例7: add
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
public boolean add(T value) {
while (true) {
// find insert position
NodesPair p = find(value);
if (p.found != null && p.found.value.compareTo(value) == 0) {
return false;
} else {
// create new node
ListNode newNode = new ListNode(value, new AtomicMarkableReference<ListNode>(p.found, false));
// insert with CAS
if (p.prev.nextRef.compareAndSet(p.found, newNode, false, false)) {
return true;
}
}
}
}
示例8: add
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
@Override
public boolean add(T value) {
while (true) {
PredCurrPair pair = find(value);
Node pred = pair.pred;
Node curr = pair.curr;
if (curr != null && curr.key.compareTo(value) == 0) {
return false;
}
Node node = new Node(value, new AtomicMarkableReference<>(curr, false));
if (pred.next.compareAndSet(curr, node, false, false)) {
return true;
}
}
}
示例9: add
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
@Override
public boolean add(T value) {
while (true) {
SlidingWindow<T> window = SlidingWindow.find(head, value);
Node<T> previous = window.previous,
current = window.current;
// добавляемое значение уже существует, возвращаем отказ
if (current != null && current.value.compareTo(value) == 0) {
return false;
}
// если мы добавили, возвращаем успех
Node<T> next = new Node<>(value, new AtomicMarkableReference<>(current, false));
if (previous.next.compareAndSet(current, next, false, false)) {
return true;
}
}
}
示例10: Finding
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
Finding(T value) {
retry: while (true) {
prev = head;
curr = head.reference.getReference();
Node succ;
while (curr != null) {
AtomicMarkableReference<Node> ref = curr.reference;
succ = ref.getReference();
if (ref.isMarked()) { // try to delete physically
if (!prev.reference.compareAndSet(curr, succ, false, false)) {
continue retry;
}
curr = succ;
} else {
if (curr.value.compareTo(value) >= 0) return;
prev = curr;
curr = succ;
}
}
return;
}
}
示例11: 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;
}
}
示例12: 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;
}
}
示例13: 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();
}
}
}
}
示例14: add
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
@Override
public boolean add(T value) {
while (true) {
NodePair finded = find(value);
Node curr = finded.first, prev = finded.second;
if (curr != null && curr.value.compareTo(value) == 0) {
return false;
}
Node new_node = new Node(value, new AtomicMarkableReference<Node>(null, false));
if (prev == null) {
if (head.compareAndSet(null, new_node, false, false)) {
size.incrementAndGet();
return true;
}
} else {
if (prev.next.compareAndSet(curr, new_node, false, false)) {
size.incrementAndGet();
return true;
}
}
}
}
示例15: add
import java.util.concurrent.atomic.AtomicMarkableReference; //导入依赖的package包/类
public boolean add(T item)
{
while (true)
{
Pair pair_lc = search(item);
Node left = pair_lc.left;
Node curr = pair_lc.curr;
if(curr.item != null && curr.item.compareTo(item) == 0)
return false;
else
{
Node node = new Node(item);
node.next = new AtomicMarkableReference(curr, false);
if (left.next.compareAndSet(curr, node, false, false))
return true;
}
}
}