Java Code Examples for java.util.concurrent.atomic.AtomicStampedReference#compareAndSet()
The following examples show how to use
java.util.concurrent.atomic.AtomicStampedReference#compareAndSet() .
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 openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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 2
Source File: AtomicStampedReferenceTest.java From openjdk-jdk9 with GNU General Public License v2.0 | 6 votes |
/** * 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 3
Source File: AtomicStampedReferenceTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * 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 4
Source File: AtomicStampedReferenceTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * 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 5
Source File: ControlledProcessState.java From wildfly-core with GNU Lesser General Public License v2.1 | 6 votes |
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 6
Source File: ControlledProcessState.java From wildfly-core with GNU Lesser General Public License v2.1 | 6 votes |
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 7
Source File: Lesson5_7.java From java-interview with Apache License 2.0 | 4 votes |
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()); }