java.util.concurrent.atomic.AtomicIntegerFieldUpdater Java Examples

The following examples show how to use java.util.concurrent.atomic.AtomicIntegerFieldUpdater. 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: NettyPipelinedConnection.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
/**
 * Offer {@code item} to the queue, try to acquire the processing lock, and if successful return an item for
 * single-consumer style processing. If non-{@code null} is returned the caller is responsible for releasing
 * the lock!
 * @param queue The {@link Queue#offer(Object)} and {@link Queue#poll()} (assuming lock was acquired).
 * @param lockUpdater Used to acquire the lock via
 * {@link ConcurrentUtils#tryAcquireLock(AtomicIntegerFieldUpdater, Object)}.
 * @param item The item to {@link Queue#offer(Object)}.
 * @param <T> The type of item in the {@link Queue}.
 * @return {@code null} if the queue was empty, or the lock couldn't be acquired. otherwise the lock has been
 * acquired and it is the caller's responsibility to release!
 */
@Nullable
private <T> T addAndTryPoll(final Queue<T> queue,
    @SuppressWarnings("rawtypes") final AtomicIntegerFieldUpdater<NettyPipelinedConnection> lockUpdater, T item) {
    queue.add(item);
    while (tryAcquireLock(lockUpdater, this)) {
        // exceptions are not expected from poll, and if they occur we can't reliably recover which would involve
        // draining the queue. just throw with the lock poisoned, callers will propagate the exception to related
        // subscriber and close the connection.
        final T next = queue.poll();
        if (next != null) {
            return next; // lock must be released when the returned task completes!
        } else if (releaseLock(lockUpdater, this)) {
            return null;
        }
    }
    return null;
}
 
Example #2
Source File: NettyPipelinedConnection.java    From servicetalk with Apache License 2.0 6 votes vote down vote up
/**
 * Poll the {@code queue} and attempt to process an item. The lock must be acquired on entry into this method and
 * if this method return non-{@code null} the lock will not be released (caller's responsibility to later release)
 * to continue the single-consumer style processing.
 * @param queue The queue to {@link Queue#poll()}.
 * @param lockUpdater Used to release via
 * {@link ConcurrentUtils#releaseLock(AtomicIntegerFieldUpdater, Object)} if the queue is empty
 * @param <T> The type of item in the {@link Queue}.
 * @return {@code null} if the queue was empty. otherwise the lock remains acquired and it is the caller's
 * responsibility to release (via subsequent calls to this method).
 */
@Nullable
private <T> T pollWithLockAcquired(final Queue<T> queue,
           @SuppressWarnings("rawtypes") final AtomicIntegerFieldUpdater<NettyPipelinedConnection> lockUpdater) {
    // the lock has been acquired!
    try {
        do {
            final T next = queue.poll();
            if (next != null) {
                return next; // lock must be released when the returned task completes!
            } else if (releaseLock(lockUpdater, this)) {
                return null;
            }
        } while (tryAcquireLock(lockUpdater, this));

        return null;
    } catch (Throwable cause) {
        // exceptions are not expected from poll, and if they occur we can't reliably recover which would involve
        // draining the queue. just throw with the lock poisoned and close the connection.
        connection.closeAsync().subscribe();
        throw cause;
    }
}
 
Example #3
Source File: AtomicIntegerFieldUpdaterTest.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 value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    x = 1;
    final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");

    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3))
                Thread.yield();
        }});

    t.start();
    assertTrue(a.compareAndSet(this, 1, 2));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertEquals(3, a.get(this));
}
 
Example #4
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * compareAndSet in one thread enables another waiting for value
 * to succeed
 */
public void testCompareAndSetInMultipleThreads() throws Exception {
    x = 1;
    final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");

    Thread t = new Thread(new CheckedRunnable() {
        public void realRun() {
            while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3))
                Thread.yield();
        }});

    t.start();
    assertTrue(a.compareAndSet(this, 1, 2));
    t.join(LONG_DELAY_MS);
    assertFalse(t.isAlive());
    assertEquals(3, a.get(this));
}
 
Example #5
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getAndAdd returns previous value and adds given value
 */
public void testGetAndAdd() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.getAndAdd(this, 2));
    assertEquals(3, a.get(this));
    assertEquals(3, a.getAndAdd(this, -4));
    assertEquals(-1, a.get(this));
}
 
Example #6
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * get returns the last value lazySet by same thread
 */
public void testGetLazySet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.get(this));
    a.lazySet(this, 2);
    assertEquals(2, a.get(this));
    a.lazySet(this, -3);
    assertEquals(-3, a.get(this));
}
 
Example #7
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * get returns the last value set or assigned
 */
public void testGetSet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.get(this));
    a.set(this, 2);
    assertEquals(2, a.get(this));
    a.set(this, -3);
    assertEquals(-3, a.get(this));
}
 
Example #8
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * compareAndSet succeeds in changing value if equal to expected else fails
 */
public void testCompareAndSet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertTrue(a.compareAndSet(this, 1, 2));
    assertTrue(a.compareAndSet(this, 2, -4));
    assertEquals(-4, a.get(this));
    assertFalse(a.compareAndSet(this, -5, 7));
    assertEquals(-4, a.get(this));
    assertTrue(a.compareAndSet(this, -4, 7));
    assertEquals(7, a.get(this));
}
 
Example #9
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * AtomicIntegerFieldUpdater accumulateAndGet updates with supplied
 * function and returns result.
 */
public void testIntegerFieldUpdaterAccumulateAndGet() {
    AtomicIntegerFieldUpdater a = anIntFieldUpdater();
    a.set(this, 1);
    assertEquals(7, a.accumulateAndGet(this, 6, Integer::sum));
    assertEquals(10, a.accumulateAndGet(this, 3, Integer::sum));
    assertEquals(10, a.get(this));
    assertEquals(10, anIntField);
}
 
Example #10
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * AtomicIntegerFieldUpdater getAndAccumulate returns previous value
 * and updates with supplied function.
 */
public void testIntegerFieldUpdaterGetAndAccumulate() {
    AtomicIntegerFieldUpdater a = anIntFieldUpdater();
    a.set(this, 1);
    assertEquals(1, a.getAndAccumulate(this, 2, Integer::sum));
    assertEquals(3, a.getAndAccumulate(this, 3, Integer::sum));
    assertEquals(6, a.get(this));
    assertEquals(6, anIntField);
}
 
Example #11
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * AtomicIntegerFieldUpdater updateAndGet updates with supplied function and
 * returns result.
 */
public void testIntegerFieldUpdaterUpdateAndGet() {
    AtomicIntegerFieldUpdater a = anIntFieldUpdater();
    a.set(this, 1);
    assertEquals(18, a.updateAndGet(this, Atomic8Test::addInt17));
    assertEquals(35, a.updateAndGet(this, Atomic8Test::addInt17));
    assertEquals(35, a.get(this));
    assertEquals(35, anIntField);
}
 
Example #12
Source File: PlatformDependent.java    From netty4.0.27Learn with Apache License 2.0 5 votes vote down vote up
/**
 * Create a new optimized {@link AtomicIntegerFieldUpdater} or {@code null} if it
 * could not be created. Because of this the caller need to check for {@code null} and if {@code null} is returned
 * use {@link AtomicIntegerFieldUpdater#newUpdater(Class, String)} as fallback.
 */
public static <T> AtomicIntegerFieldUpdater<T> newAtomicIntegerFieldUpdater(
        Class<?> tclass, String fieldName) {
    if (hasUnsafe()) {
        try {
            return PlatformDependent0.newAtomicIntegerFieldUpdater(tclass, fieldName);
        } catch (Throwable ignore) {
            // ignore
        }
    }
    return null;
}
 
Example #13
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * repeated weakCompareAndSet succeeds in changing value when equal
 * to expected
 */
public void testWeakCompareAndSet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    do {} while (!a.weakCompareAndSet(this, 1, 2));
    do {} while (!a.weakCompareAndSet(this, 2, -4));
    assertEquals(-4, a.get(this));
    do {} while (!a.weakCompareAndSet(this, -4, 7));
    assertEquals(7, a.get(this));
}
 
Example #14
Source File: AtomicUpdaterFactory.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns an updater for objects with the given integer field.
 */
public static <T> AtomicIntegerFieldUpdater<T> newIntegerFieldUpdater(
    Class<T> tclass, String fieldName) {
  if (UnsafeHolder.hasUnsafe()) {
    return new UnsafeAtomicIntegerFieldUpdater<T>(tclass, fieldName);
  }
  else {
    return AtomicIntegerFieldUpdater.newUpdater(tclass, fieldName);
  }
}
 
Example #15
Source File: PersistentDispatcherFailoverConsumerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private Consumer getNextConsumer(PersistentDispatcherMultipleConsumers dispatcher) throws Exception {

    Consumer consumer = dispatcher.getNextConsumer();

    if (consumer != null) {
        Field field = Consumer.class.getDeclaredField("MESSAGE_PERMITS_UPDATER");
        field.setAccessible(true);
        AtomicIntegerFieldUpdater<Consumer> messagePermits = (AtomicIntegerFieldUpdater<Consumer>) field.get(consumer);
        messagePermits.decrementAndGet(consumer);
        return consumer;
    }
    return null;
}
 
Example #16
Source File: Metrics.java    From mangooio with Apache License 2.0 5 votes vote down vote up
public void reset() {
    this.maxRequestTimeUpdater = AtomicIntegerFieldUpdater.newUpdater(Metrics.class, "maxRequestTime");
    this.minRequestTimeUpdater = AtomicIntegerFieldUpdater.newUpdater(Metrics.class, "minRequestTime");
    this.totalRequestTimeUpdater = AtomicLongFieldUpdater.newUpdater(Metrics.class, "totalRequestTime");
    this.totalRequestsUpdater = AtomicLongFieldUpdater.newUpdater(Metrics.class, "totalRequests");
    this.responseCount = new ConcurrentHashMap<>(INITIAL_CAPACITY, LOAD_FACTOR, CONCURRENCY_LEVEL);
    this.dataSend = new AtomicLong();
    this.avgRequestTime = 0;
    this.totalRequestTime = 0;
    this.totalRequests = 0;
    this.maxRequestTime = -1;
    this.minRequestTime = -1;
}
 
Example #17
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * compareAndSet succeeds in changing protected field value if
 * equal to expected else fails
 */
public void testCompareAndSetProtected() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("protectedField");
    protectedField = 1;
    assertTrue(a.compareAndSet(this, 1, 2));
    assertTrue(a.compareAndSet(this, 2, -4));
    assertEquals(-4, a.get(this));
    assertFalse(a.compareAndSet(this, -5, 7));
    assertEquals(-4, a.get(this));
    assertTrue(a.compareAndSet(this, -4, 7));
    assertEquals(7, a.get(this));
}
 
Example #18
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * AtomicIntegerFieldUpdater getAndUpdate returns previous value and updates
 * result of supplied function
 */
public void testIntegerFieldUpdaterGetAndUpdate() {
    AtomicIntegerFieldUpdater a = anIntFieldUpdater();
    a.set(this, 1);
    assertEquals(1, a.getAndUpdate(this, Atomic8Test::addInt17));
    assertEquals(18, a.getAndUpdate(this, Atomic8Test::addInt17));
    assertEquals(35, a.get(this));
    assertEquals(35, anIntField);
}
 
Example #19
Source File: LoadBalancerTest.java    From pulsar with Apache License 2.0 5 votes vote down vote up
/**
 * Ensure that the load manager's zookeeper data cache is shutdown after invoking stop().
 */
@Test
public void testStop() throws Exception {
    final SimpleLoadManagerImpl loadManager = (SimpleLoadManagerImpl) pulsarServices[0].getLoadManager().get();
    loadManager.stop();
    Field loadReportCacheField = SimpleLoadManagerImpl.class.getDeclaredField("loadReportCacheZk");
    loadReportCacheField.setAccessible(true);
    ZooKeeperDataCache<LoadReport> loadReportCache = (ZooKeeperDataCache<LoadReport>) loadReportCacheField
            .get(loadManager);
    Field IS_SHUTDOWN_UPDATER = ZooKeeperDataCache.class.getDeclaredField("IS_SHUTDOWN_UPDATER");
    IS_SHUTDOWN_UPDATER.setAccessible(true);
    final int TRUE = 1;
    assert (((AtomicIntegerFieldUpdater<ZooKeeperDataCache>) (IS_SHUTDOWN_UPDATER.get(loadReportCache))).get(loadReportCache) == TRUE);
}
 
Example #20
Source File: ConcurrentMapOpsTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** compare raw numbers for atomic ops using JDK vs unsafe wrapper classes */
public void SW_testCompareAtomicOps() {

  final AtomicIntegerFieldUpdater<ConcurrentMapOpsTest> intJDKCounter =
      AtomicIntegerFieldUpdater.newUpdater(ConcurrentMapOpsTest.class,
          "intJDKCounter");
  final AtomicLongFieldUpdater<ConcurrentMapOpsTest> longJDKCounter =
      AtomicLongFieldUpdater.newUpdater(ConcurrentMapOpsTest.class,
          "longJDKCounter");
  final AtomicReferenceFieldUpdater<ConcurrentMapOpsTest, LongRef>
      refJDKCounter = AtomicReferenceFieldUpdater.newUpdater(
          ConcurrentMapOpsTest.class, LongRef.class, "refJDKCounter");

  final AtomicIntegerFieldUpdater<ConcurrentMapOpsTest> intUnsafeCounter =
      AtomicUpdaterFactory.newIntegerFieldUpdater(ConcurrentMapOpsTest.class,
          "intUnsafeCounter");
  final AtomicLongFieldUpdater<ConcurrentMapOpsTest> longUnsafeCounter =
      AtomicUpdaterFactory.newLongFieldUpdater(ConcurrentMapOpsTest.class,
          "longUnsafeCounter");
  final AtomicReferenceFieldUpdater<ConcurrentMapOpsTest, LongRef>
      refUnsafeCounter = AtomicUpdaterFactory.newReferenceFieldUpdater(
          ConcurrentMapOpsTest.class, LongRef.class, "refUnsafeCounter");

  // some warmups
  runAtomicOps(1, 50000, intJDKCounter, longJDKCounter, refJDKCounter,
      intUnsafeCounter, longUnsafeCounter, refUnsafeCounter);

  // timed runs with single threads to see the raw overheads with no
  // concurrency (as we would expect in most usual cases)
  runAtomicOps(1, 50000000, intJDKCounter, longJDKCounter, refJDKCounter,
      intUnsafeCounter, longUnsafeCounter, refUnsafeCounter);

  // now with concurrency
  runAtomicOps(5, 2000000, intJDKCounter, longJDKCounter, refJDKCounter,
      intUnsafeCounter, longUnsafeCounter, refUnsafeCounter);
}
 
Example #21
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getAndSet returns previous value and sets to given value
 */
public void testGetAndSet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.getAndSet(this, 0));
    assertEquals(0, a.getAndSet(this, -10));
    assertEquals(-10, a.getAndSet(this, 1));
}
 
Example #22
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getAndAdd returns previous value and adds given value
 */
public void testGetAndAdd() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.getAndAdd(this, 2));
    assertEquals(3, a.get(this));
    assertEquals(3, a.getAndAdd(this, -4));
    assertEquals(-1, a.get(this));
}
 
Example #23
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getAndDecrement returns previous value and decrements
 */
public void testGetAndDecrement() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.getAndDecrement(this));
    assertEquals(0, a.getAndDecrement(this));
    assertEquals(-1, a.getAndDecrement(this));
}
 
Example #24
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * getAndIncrement returns previous value and increments
 */
public void testGetAndIncrement() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.getAndIncrement(this));
    assertEquals(2, a.get(this));
    a.set(this, -2);
    assertEquals(-2, a.getAndIncrement(this));
    assertEquals(-1, a.getAndIncrement(this));
    assertEquals(0, a.getAndIncrement(this));
    assertEquals(1, a.get(this));
}
 
Example #25
Source File: AtomicUpdaterFactory.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns an updater for objects with the given integer field.
 */
public static <T> AtomicIntegerFieldUpdater<T> newIntegerFieldUpdater(
    Class<T> tclass, String fieldName) {
  if (UnsafeHolder.hasUnsafe()) {
    return new UnsafeAtomicIntegerFieldUpdater<T>(tclass, fieldName);
  }
  else {
    return AtomicIntegerFieldUpdater.newUpdater(tclass, fieldName);
  }
}
 
Example #26
Source File: ConcurrentMapOpsTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
/** compare raw numbers for atomic ops using JDK vs unsafe wrapper classes */
public void SW_testCompareAtomicOps() {

  final AtomicIntegerFieldUpdater<ConcurrentMapOpsTest> intJDKCounter =
      AtomicIntegerFieldUpdater.newUpdater(ConcurrentMapOpsTest.class,
          "intJDKCounter");
  final AtomicLongFieldUpdater<ConcurrentMapOpsTest> longJDKCounter =
      AtomicLongFieldUpdater.newUpdater(ConcurrentMapOpsTest.class,
          "longJDKCounter");
  final AtomicReferenceFieldUpdater<ConcurrentMapOpsTest, LongRef>
      refJDKCounter = AtomicReferenceFieldUpdater.newUpdater(
          ConcurrentMapOpsTest.class, LongRef.class, "refJDKCounter");

  final AtomicIntegerFieldUpdater<ConcurrentMapOpsTest> intUnsafeCounter =
      AtomicUpdaterFactory.newIntegerFieldUpdater(ConcurrentMapOpsTest.class,
          "intUnsafeCounter");
  final AtomicLongFieldUpdater<ConcurrentMapOpsTest> longUnsafeCounter =
      AtomicUpdaterFactory.newLongFieldUpdater(ConcurrentMapOpsTest.class,
          "longUnsafeCounter");
  final AtomicReferenceFieldUpdater<ConcurrentMapOpsTest, LongRef>
      refUnsafeCounter = AtomicUpdaterFactory.newReferenceFieldUpdater(
          ConcurrentMapOpsTest.class, LongRef.class, "refUnsafeCounter");

  // some warmups
  runAtomicOps(1, 50000, intJDKCounter, longJDKCounter, refJDKCounter,
      intUnsafeCounter, longUnsafeCounter, refUnsafeCounter);

  // timed runs with single threads to see the raw overheads with no
  // concurrency (as we would expect in most usual cases)
  runAtomicOps(1, 50000000, intJDKCounter, longJDKCounter, refJDKCounter,
      intUnsafeCounter, longUnsafeCounter, refUnsafeCounter);

  // now with concurrency
  runAtomicOps(5, 2000000, intJDKCounter, longJDKCounter, refJDKCounter,
      intUnsafeCounter, longUnsafeCounter, refUnsafeCounter);
}
 
Example #27
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * incrementAndGet increments and returns current value
 */
public void testIncrementAndGet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(2, a.incrementAndGet(this));
    assertEquals(2, a.get(this));
    a.set(this, -2);
    assertEquals(-1, a.incrementAndGet(this));
    assertEquals(0, a.incrementAndGet(this));
    assertEquals(1, a.incrementAndGet(this));
    assertEquals(1, a.get(this));
}
 
Example #28
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * decrementAndGet decrements and returns current value
 */
public void testDecrementAndGet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(0, a.decrementAndGet(this));
    assertEquals(-1, a.decrementAndGet(this));
    assertEquals(-2, a.decrementAndGet(this));
    assertEquals(-2, a.get(this));
}
 
Example #29
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * addAndGet adds given value to current, and returns current value
 */
public void testAddAndGet() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(3, a.addAndGet(this, 2));
    assertEquals(3, a.get(this));
    assertEquals(-1, a.addAndGet(this, -4));
    assertEquals(-1, a.get(this));
}
 
Example #30
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * getAndIncrement returns previous value and increments
 */
public void testGetAndIncrement() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a;
    a = updaterFor("x");
    x = 1;
    assertEquals(1, a.getAndIncrement(this));
    assertEquals(2, a.get(this));
    a.set(this, -2);
    assertEquals(-2, a.getAndIncrement(this));
    assertEquals(-1, a.getAndIncrement(this));
    assertEquals(0, a.getAndIncrement(this));
    assertEquals(1, a.get(this));
}