當前位置: 首頁>>代碼示例>>Java>>正文


Java AtomicReferenceFieldUpdater類代碼示例

本文整理匯總了Java中java.util.concurrent.atomic.AtomicReferenceFieldUpdater的典型用法代碼示例。如果您正苦於以下問題:Java AtomicReferenceFieldUpdater類的具體用法?Java AtomicReferenceFieldUpdater怎麽用?Java AtomicReferenceFieldUpdater使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


AtomicReferenceFieldUpdater類屬於java.util.concurrent.atomic包,在下文中一共展示了AtomicReferenceFieldUpdater類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testAPI

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * Basic tests of the API : Simple function calls to exercise all of the functions listed in the API
 * specification for the AtomicReferenceFieldUpdater class.
 */	
public void testAPI()
{
	// =================================================================================
	// Create instances of AtomicReferenceFieldUpdater to work with
	
	for(int i = 0; i < updaters.length; i++)
	{
		updaters[i] = AtomicReferenceFieldUpdater.newUpdater(AtomicTestObject.class, String.class, "volatileString");
	}
	
	// =================================================================================
	// Basic API tests 
	
	assertEquals("1 : get()", "the answer", getRandomUpdater().get(testObject1));
	assertEquals("2 : get()", getRandomUpdater().get(testObject1), getRandomUpdater().get(testObject2));
	assertEquals("3 : get()", getRandomUpdater().get(testObject1), getRandomUpdater().get(testObject1));
	assertEquals("4 : get()", getRandomUpdater().get(testObject2), getRandomUpdater().get(testObject2));
	
	assertEquals("5 : getAndSet()", "the answer", getRandomUpdater().getAndSet(testObject1, "the question"));
	assertEquals("6 : get()", "the question", getRandomUpdater().get(testObject1));
	assertEquals("7 : getAndSet()", "the question", getRandomUpdater().getAndSet(testObject1, "the answer"));
	
	assertEquals("8 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject1, "the answer", "the question"));
	assertEquals("9 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject2, "the answer", "the question"));
	assertEquals("10: get()", getRandomUpdater().get(testObject1), getRandomUpdater().get(testObject2));
	assertEquals("11: compareAndSet()", false, getRandomUpdater().compareAndSet(testObject1, "the answer", "the question"));
	assertEquals("12: compareAndSet()", true, getRandomUpdater().compareAndSet(testObject1, "the question", "the answer"));		
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:33,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例2: testCompareAndSetInMultipleThreads

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    x = one;
    final AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> a;
    a = updaterFor("x");

    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!a.compareAndSet(AtomicReferenceFieldUpdaterTest.this, two, three))
                Thread.yield();
        }});

    t.start();
    assertTrue(a.compareAndSet(this, one, two));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertSame(three, a.get(this));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:22,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例3: set

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * Atomically push the field to a {@link Disposable} and dispose the old content.
 *
 * @param updater the target field updater
 * @param holder the target instance holding the field
 * @param newValue the new Disposable to push
 * @return true if successful, false if the field contains the {@link #DISPOSED} instance.
 */
static <T> boolean set(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, @Nullable Disposable newValue) {
	for (;;) {
		Disposable current = updater.get(holder);
		if (current == DISPOSED) {
			if (newValue != null) {
				newValue.dispose();
			}
			return false;
		}
		if (updater.compareAndSet(holder, current, newValue)) {
			if (current != null) {
				current.dispose();
			}
			return true;
		}
	}
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:26,代碼來源:Disposables.java

示例4: set

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * Atomically push the field to a {@link Disposable} and dispose the old content.
 *
 * @param updater the target field updater
 * @param holder the target instance holding the field
 * @param newValue the new Disposable to push
 * @return true if successful, false if the field contains the {@link #DISPOSED} instance.
 */
public static <T> boolean set(AtomicReferenceFieldUpdater<T, Disposable> updater, T holder, @Nullable Disposable newValue) {
	for (;;) {
		Disposable current = updater.get(holder);
		if (current == DISPOSED) {
			if (newValue != null) {
				newValue.dispose();
			}
			return false;
		}
		if (updater.compareAndSet(holder, current, newValue)) {
			if (current != null) {
				current.dispose();
			}
			return true;
		}
	}
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:26,代碼來源:OperatorDisposables.java

示例5: set

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * A generic utility to atomically replace a subscription or cancel the replacement
 * if current subscription is marked as cancelled (as in {@link #cancelledSubscription()})
 * or was concurrently updated before.
 * <p>
 * The replaced subscription is itself cancelled.
 *
 * @param field The Atomic container
 * @param instance the instance reference
 * @param s the subscription
 * @param <F> the instance type
 *
 * @return true if replaced
 */
public static <F> boolean set(AtomicReferenceFieldUpdater<F, Subscription> field,
		F instance,
		Subscription s) {
	for (; ; ) {
		Subscription a = field.get(instance);
		if (a == CancelledSubscription.INSTANCE) {
			s.cancel();
			return false;
		}
		if (field.compareAndSet(instance, a, s)) {
			if (a != null) {
				a.cancel();
			}
			return true;
		}
	}
}
 
開發者ID:reactor,項目名稱:reactor-core,代碼行數:32,代碼來源:Operators.java

示例6: removeSequence

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * 原子移除某個指定的sequence
 *
 * @param holder 原子更新的域所屬的類對象
 * @param sequenceUpdater 原子更新的域對象
 * @param sequence 要移除的sequence
 * @param <T>
 * @return
 */
public static <T> boolean removeSequence(
        final T holder,
        final AtomicReferenceFieldUpdater<T, Sequence[]> sequenceUpdater,
        final Sequence sequence)
{
    int numToRemove;
    Sequence[] oldSequences;
    Sequence[] newSequences;

    do
    {
        oldSequences = sequenceUpdater.get(holder);

        numToRemove = countMatching(oldSequences, sequence);

        if (0 == numToRemove)
        {
            break;
        }

        final int oldSize = oldSequences.length;
        newSequences = new Sequence[oldSize - numToRemove];

        for (int i = 0, pos = 0; i < oldSize; i++)
        {
            final Sequence testSequence = oldSequences[i];
            if (sequence != testSequence)
            {
                newSequences[pos++] = testSequence;
            }
        }
    }
    while (!sequenceUpdater.compareAndSet(holder, oldSequences, newSequences));

    return numToRemove != 0;
}
 
開發者ID:huang-up,項目名稱:mycat-src-1.6.1-RELEASE,代碼行數:46,代碼來源:SequenceGroups.java

示例7: SafeAtomicHelper

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
SafeAtomicHelper(
    AtomicReferenceFieldUpdater<Waiter, Thread> waiterThreadUpdater,
    AtomicReferenceFieldUpdater<Waiter, Waiter> waiterNextUpdater,
    AtomicReferenceFieldUpdater<AbstractFuture, Waiter> waitersUpdater,
    AtomicReferenceFieldUpdater<AbstractFuture, Listener> listenersUpdater,
    AtomicReferenceFieldUpdater<AbstractFuture, Object> valueUpdater) {
  this.waiterThreadUpdater = waiterThreadUpdater;
  this.waiterNextUpdater = waiterNextUpdater;
  this.waitersUpdater = waitersUpdater;
  this.listenersUpdater = listenersUpdater;
  this.valueUpdater = valueUpdater;
}
 
開發者ID:zugzug90,項目名稱:guava-mock,代碼行數:13,代碼來源:AbstractFuture.java

示例8: testReferenceFieldUpdaterGetAndUpdate

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * AtomicReferenceFieldUpdater getAndUpdate returns previous value
 * and updates result of supplied function
 */
public void testReferenceFieldUpdaterGetAndUpdate() {
    AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
    a.set(this, one);
    assertEquals((Integer) 1, a.getAndUpdate(this, Atomic8Test::addInteger17));
    assertEquals((Integer) 18, a.getAndUpdate(this, Atomic8Test::addInteger17));
    assertEquals((Integer) 35, a.get(this));
    assertEquals((Integer) 35, anIntegerField);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:Atomic8Test.java

示例9: testReferenceFieldUpdaterUpdateAndGet

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * AtomicReferenceFieldUpdater updateAndGet updates with supplied
 * function and returns result.
 */
public void testReferenceFieldUpdaterUpdateAndGet() {
    AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
    a.set(this, one);
    assertEquals((Integer) 18, a.updateAndGet(this, Atomic8Test::addInteger17));
    assertEquals((Integer) 35, a.updateAndGet(this, Atomic8Test::addInteger17));
    assertEquals((Integer) 35, a.get(this));
    assertEquals((Integer) 35, anIntegerField);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:Atomic8Test.java

示例10: testReferenceFieldUpdaterGetAndAccumulate

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * AtomicReferenceFieldUpdater returns previous value and updates
 * with supplied function.
 */
public void testReferenceFieldUpdaterGetAndAccumulate() {
    AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
    a.set(this, one);
    assertEquals((Integer) 1, a.getAndAccumulate(this, 2, Atomic8Test::sumInteger));
    assertEquals((Integer) 3, a.getAndAccumulate(this, 3, Atomic8Test::sumInteger));
    assertEquals((Integer) 6, a.get(this));
    assertEquals((Integer) 6, anIntegerField);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:Atomic8Test.java

示例11: testReferenceFieldUpdaterAccumulateAndGet

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * AtomicReferenceFieldUpdater accumulateAndGet updates with
 * supplied function and returns result.
 */
public void testReferenceFieldUpdaterAccumulateAndGet() {
    AtomicReferenceFieldUpdater<Atomic8Test,Integer> a = anIntegerFieldUpdater();
    a.set(this, one);
    assertEquals((Integer) 7, a.accumulateAndGet(this, 6, Atomic8Test::sumInteger));
    assertEquals((Integer) 10, a.accumulateAndGet(this, 3, Atomic8Test::sumInteger));
    assertEquals((Integer) 10, a.get(this));
    assertEquals((Integer) 10, anIntegerField);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:13,代碼來源:Atomic8Test.java

示例12: testFieldUpdaters_ClassCastException

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
/**
 * Object arguments for parameters of type T that are not
 * instances of the class passed to the newUpdater call will
 * result in a ClassCastException being thrown.
 */
public void testFieldUpdaters_ClassCastException() {
    // Use raw types to allow passing wrong object type, provoking CCE
    final AtomicLongFieldUpdater longUpdater = aLongFieldUpdater();
    final AtomicIntegerFieldUpdater intUpdater = anIntFieldUpdater();
    final AtomicReferenceFieldUpdater refUpdater = anIntegerFieldUpdater();
    final Object obj = new Object();
    for (Object x : new Object[]{ new Object(), null }) {
        Runnable[] throwingActions = {
            () -> longUpdater.get(x),
            () -> intUpdater.get(x),
            () -> refUpdater.get(x),

            () -> longUpdater.set(x, 17L),
            () -> intUpdater.set(x, 17),
            () -> refUpdater.set(x, (Integer) 17),

            () -> longUpdater.addAndGet(x, 17L),
            () -> intUpdater.addAndGet(x, 17),

            () -> longUpdater.getAndUpdate(x, y -> y),
            () -> intUpdater.getAndUpdate(x, y -> y),
            () -> refUpdater.getAndUpdate(x, y -> y),

            () -> longUpdater.compareAndSet(x, 17L, 42L),
            () -> intUpdater.compareAndSet(x, 17, 42),
            () -> refUpdater.compareAndSet(x, (Integer) 17, (Integer) 42),
        };
        assertThrows(ClassCastException.class, throwingActions);
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:36,代碼來源:Atomic8Test.java

示例13: checkPrivateAccess

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
public void checkPrivateAccess() {
    try {
        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
            AtomicReferenceFieldUpdater.newUpdater
            (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
        shouldThrow();
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例14: checkCompareAndSetProtectedSub

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
public void checkCompareAndSetProtectedSub() {
    AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
        AtomicReferenceFieldUpdater.newUpdater
        (AtomicReferenceFieldUpdaterTest.class, Integer.class, "protectedField");
    this.protectedField = one;
    assertTrue(a.compareAndSet(this, one, two));
    assertTrue(a.compareAndSet(this, two, m4));
    assertSame(m4, a.get(this));
    assertFalse(a.compareAndSet(this, m5, seven));
    assertNotSame(seven, a.get(this));
    assertTrue(a.compareAndSet(this, m4, seven));
    assertSame(seven, a.get(this));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例15: checkPackageAccess

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入依賴的package包/類
public void checkPackageAccess(AtomicReferenceFieldUpdaterTest obj) {
    obj.x = one;
    AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
        AtomicReferenceFieldUpdater.newUpdater
        (AtomicReferenceFieldUpdaterTest.class, Integer.class, "x");
    assertSame(one, a.get(obj));
    assertTrue(a.compareAndSet(obj, one, two));
    assertSame(two, a.get(obj));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:AtomicReferenceFieldUpdaterTest.java


注:本文中的java.util.concurrent.atomic.AtomicReferenceFieldUpdater類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。