java.util.concurrent.atomic.AtomicStampedReference Java Examples

The following examples show how to use java.util.concurrent.atomic.AtomicStampedReference. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AtomicStampedReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * get returns the last values of reference and stamp set
 */
public void testGetSet() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.getReference());
    assertEquals(0, ai.getStamp());
    assertSame(one, ai.get(mark));
    assertEquals(0, mark[0]);
    ai.set(two, 0);
    assertSame(two, ai.getReference());
    assertEquals(0, ai.getStamp());
    assertSame(two, ai.get(mark));
    assertEquals(0, mark[0]);
    ai.set(one, 1);
    assertSame(one, ai.getReference());
    assertEquals(1, ai.getStamp());
    assertSame(one, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #2
Source File: ControlledProcessState.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Object setRestartRequired() {
    AtomicStampedReference<State> stateRef = state;
    int newStamp = stamp.incrementAndGet();
    int[] receiver = new int[1];
    // Keep trying until stateRef is RESTART_REQUIRED with our stamp
    for (;;) {
        State was = stateRef.get(receiver);
        if (was == State.STARTING || was == State.STOPPING) {
            break;
        }
        synchronized (service) {
            if (stateRef.compareAndSet(was, State.RESTART_REQUIRED, receiver[0], newStamp)) {
                restartRequiredFlag = true;
                service.stateChanged(State.RESTART_REQUIRED);
                break;
            }
        }
    }
    return Integer.valueOf(newStamp);
}
 
Example #3
Source File: ControlledProcessState.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public Object setReloadRequired() {
    if (!reloadSupported) {
        return setRestartRequired();
    }
    AtomicStampedReference<State> stateRef = state;
    int newStamp = stamp.incrementAndGet();
    int[] receiver = new int[1];
    // Keep trying until stateRef is RELOAD_REQUIRED with our stamp
    for (;;) {
        State was = stateRef.get(receiver);
        if (was == State.STARTING || was == State.STOPPING || was == State.RESTART_REQUIRED) {
            break;
        }
        synchronized (service) {
            if (stateRef.compareAndSet(was, State.RELOAD_REQUIRED, receiver[0], newStamp)) {
                service.stateChanged(State.RELOAD_REQUIRED);
                break;
            }
        }
    }
    return Integer.valueOf(newStamp);
}
 
Example #4
Source File: ControlledProcessState.java    From wildfly-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public void setRunning() {
    AtomicStampedReference<State> stateRef = state;
    int newStamp = stamp.incrementAndGet();
    int[] receiver = new int[1];
    // Keep trying until stateRef is set with our stamp
    for (;;) {
        State was = stateRef.get(receiver);
        if (was != State.STARTING) { // AS7-1103 only transition to running from STARTING
            break;
        }
        synchronized (service) {
            State newState = restartRequiredFlag ? State.RESTART_REQUIRED : State.RUNNING;
            if (state.compareAndSet(was, newState, receiver[0], newStamp)) {
                service.stateChanged(newState);
                break;
            }
        }
    }
}
 
Example #5
Source File: AtomicStampedReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing values when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.get(mark));
    assertEquals(0, ai.getStamp());
    assertEquals(0, mark[0]);

    do {} while (!ai.weakCompareAndSet(one, two, 0, 0));
    assertSame(two, ai.get(mark));
    assertEquals(0, mark[0]);

    do {} while (!ai.weakCompareAndSet(two, m3, 0, 1));
    assertSame(m3, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #6
Source File: AtomicStampedReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for stamp value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads2() throws Exception {
    final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(one, one, 1, 2))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, one, 0, 1));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(one, ai.getReference());
    assertEquals(2, ai.getStamp());
}
 
Example #7
Source File: AtomicStampedReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for reference value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(two, three, 0, 0))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, two, 0, 0));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, ai.getReference());
    assertEquals(0, ai.getStamp());
}
 
Example #8
Source File: AtomicStampedReferenceTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet succeeds in changing values if equal to expected reference
 * and stamp else fails
 */
public void testCompareAndSet() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.get(mark));
    assertEquals(0, ai.getStamp());
    assertEquals(0, mark[0]);

    assertTrue(ai.compareAndSet(one, two, 0, 0));
    assertSame(two, ai.get(mark));
    assertEquals(0, mark[0]);

    assertTrue(ai.compareAndSet(two, m3, 0, 1));
    assertSame(m3, ai.get(mark));
    assertEquals(1, mark[0]);

    assertFalse(ai.compareAndSet(two, m3, 1, 1));
    assertSame(m3, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #9
Source File: AtomicStampedReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing values when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.get(mark));
    assertEquals(0, ai.getStamp());
    assertEquals(0, mark[0]);

    do {} while (!ai.weakCompareAndSet(one, two, 0, 0));
    assertSame(two, ai.get(mark));
    assertEquals(0, mark[0]);

    do {} while (!ai.weakCompareAndSet(two, m3, 0, 1));
    assertSame(m3, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #10
Source File: AtomicStampedReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for stamp value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads2() throws Exception {
    final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(one, one, 1, 2))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, one, 0, 1));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(one, ai.getReference());
    assertEquals(2, ai.getStamp());
}
 
Example #11
Source File: AtomicStampedReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for reference value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!ai.compareAndSet(two, three, 0, 0))
                Thread.yield();
        }});

    t.start();
    assertTrue(ai.compareAndSet(one, two, 0, 0));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, ai.getReference());
    assertEquals(0, ai.getStamp());
}
 
Example #12
Source File: AtomicStampedReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * compareAndSet succeeds in changing values if equal to expected reference
 * and stamp else fails
 */
public void testCompareAndSet() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.get(mark));
    assertEquals(0, ai.getStamp());
    assertEquals(0, mark[0]);

    assertTrue(ai.compareAndSet(one, two, 0, 0));
    assertSame(two, ai.get(mark));
    assertEquals(0, mark[0]);

    assertTrue(ai.compareAndSet(two, m3, 0, 1));
    assertSame(m3, ai.get(mark));
    assertEquals(1, mark[0]);

    assertFalse(ai.compareAndSet(two, m3, 1, 1));
    assertSame(m3, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #13
Source File: AtomicStampedReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * get returns the last values of reference and stamp set
 */
public void testGetSet() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.getReference());
    assertEquals(0, ai.getStamp());
    assertSame(one, ai.get(mark));
    assertEquals(0, mark[0]);
    ai.set(two, 0);
    assertSame(two, ai.getReference());
    assertEquals(0, ai.getStamp());
    assertSame(two, ai.get(mark));
    assertEquals(0, mark[0]);
    ai.set(one, 1);
    assertSame(one, ai.getReference());
    assertEquals(1, ai.getStamp());
    assertSame(one, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #14
Source File: AtomicStampedReferenceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * constructor initializes to given reference and stamp
 */
public void testConstructor() {
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.getReference());
    assertEquals(0, ai.getStamp());
    AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
    assertNull(a2.getReference());
    assertEquals(1, a2.getStamp());
}
 
Example #15
Source File: AtomicStampedReferenceTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * attemptStamp succeeds in single thread
 */
public void testAttemptStamp() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertEquals(0, ai.getStamp());
    assertTrue(ai.attemptStamp(one, 1));
    assertEquals(1, ai.getStamp());
    assertSame(one, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #16
Source File: CompositeLock.java    From common-utils with GNU General Public License v2.0 5 votes vote down vote up
public CompositeLock() {
    tail = new AtomicStampedReference<QNode>(null, 0);
    random = new Random();
    waiting = new QNode[SIZE];
    for (int i = 0; i < waiting.length; i++) {
        waiting[i] = new QNode();
    }
}
 
Example #17
Source File: AtomicStampedReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * attemptStamp succeeds in single thread
 */
public void testAttemptStamp() {
    int[] mark = new int[1];
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertEquals(0, ai.getStamp());
    assertTrue(ai.attemptStamp(one, 1));
    assertEquals(1, ai.getStamp());
    assertSame(one, ai.get(mark));
    assertEquals(1, mark[0]);
}
 
Example #18
Source File: AtomicStampedReferenceTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * constructor initializes to given reference and stamp
 */
public void testConstructor() {
    AtomicStampedReference ai = new AtomicStampedReference(one, 0);
    assertSame(one, ai.getReference());
    assertEquals(0, ai.getStamp());
    AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
    assertNull(a2.getReference());
    assertEquals(1, a2.getStamp());
}
 
Example #19
Source File: FragmentHandler.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
FragmentHandler(FragmentHandle handle, long evictionDelayMillis) {
  this.handle = handle;
  this.evictionDelayMillis = evictionDelayMillis;
  expirationTime = System.currentTimeMillis() + evictionDelayMillis;
  execReference = new AtomicStampedReference<FragmentExecutor>(null, defaultExecStamp);
}
 
Example #20
Source File: Lesson5_7.java    From java-interview with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // 共享锁演示
    final MyReadWriteLock rwLock = new MyReadWriteLock();
    // 创建读锁 r1 和 r2
    Thread r1 = new Thread(new Runnable() {
        @Override
        public void run() {
            rwLock.read();
        }
    }, "r1");
    Thread r2 = new Thread(new Runnable() {
        @Override
        public void run() {
            rwLock.read();
        }
    }, "r2");
    r1.start();
    r2.start();
    // 等待同时读取线程执行完成
    r1.join();
    r2.join();
    // 开启写锁的操作
    new Thread(new Runnable() {
        @Override
        public void run() {
            rwLock.write();
        }
    }, "w1").start();
    new Thread(new Runnable() {
        @Override
        public void run() {
            rwLock.write();
        }
    }, "w2").start();
    // AtomicStampedReference(解决 ABA 问题)使用演示
    String name = "老王";
    String newName = "Java";
    AtomicStampedReference<String> as = new AtomicStampedReference<String>(name, 1);
    System.out.println("值:" + as.getReference() + " | Stamp:" + as.getStamp());
    as.compareAndSet(name, newName, as.getStamp(), as.getStamp() + 1);
    System.out.println("值:" + as.getReference() + " | Stamp:" + as.getStamp());
}