compareAndSet()源码实例Demo

java.util.concurrent.atomic.AtomicMarkableReference#compareAndSet()源码实例Demo

下面列出了java.util.concurrent.atomic.AtomicMarkableReference#compareAndSet() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

/**
 * 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());
}
 
/**
 * 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 项目: j2objc   文件: AtomicMarkableReferenceTest.java
/**
 * 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 项目: j2objc   文件: AtomicMarkableReferenceTest.java
/**
 * 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 项目: JavaCommon   文件: AtomicMarkableReferenceDemo.java
public static void main(String[] args) {
    Integer a = 1;
    AtomicMarkableReference atomicMarkableReference = new AtomicMarkableReference(a, false);
    atomicMarkableReference.compareAndSet(1, 2, false, true);
}
 
源代码评论
动弹
沙发等你来抢
 同类方法