Java 类java.util.concurrent.atomic.AtomicIntegerFieldUpdater 实例源码

项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:pinenut    文件:AtomicIntegerFieldUpdaterTest.java   
@Test
public void test() throws InterruptedException {
  AtomicIntegerFieldUpdater age = AtomicIntegerFieldUpdater.newUpdater(User.class, "age");

  User user = new User();


  ExecutorService executorService = Executors.newFixedThreadPool(10);
  for (int i = 0; i < 100; i++) {
    executorService.execute(() -> {
      age.addAndGet(user, 1);
    });
  }

  TimeUnit.MILLISECONDS.sleep(10);
  System.out.println(user.getAge());

}
项目:openjdk-jdk10    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk-jdk10    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk-jdk10    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk-jdk10    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk-jdk10    文件:Atomic8Test.java   
/**
 * 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);
    }
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkPrivateAccess() {
    try {
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
            AtomicIntegerFieldUpdater.newUpdater
            (AtomicIntegerFieldUpdaterTest.class, "privateField");
        shouldThrow();
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkCompareAndSetProtectedSub() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
        AtomicIntegerFieldUpdater.newUpdater
        (AtomicIntegerFieldUpdaterTest.class, "protectedField");
    this.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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkPackageAccess(AtomicIntegerFieldUpdaterTest obj) {
    obj.x = 72;
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
        AtomicIntegerFieldUpdater.newUpdater
        (AtomicIntegerFieldUpdaterTest.class, "x");
    assertEquals(72, a.get(obj));
    assertTrue(a.compareAndSet(obj, 72, 73));
    assertEquals(73, a.get(obj));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj) {
    try {
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
            AtomicIntegerFieldUpdater.newUpdater
            (AtomicIntegerFieldUpdaterTest.class, "privateField");
        throw new AssertionError("should throw");
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk-jdk10    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk9    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk9    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk9    文件:Atomic8Test.java   
/**
 * 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);
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkPrivateAccess() {
    try {
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
            AtomicIntegerFieldUpdater.newUpdater
            (AtomicIntegerFieldUpdaterTest.class, "privateField");
        shouldThrow();
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkCompareAndSetProtectedSub() {
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
        AtomicIntegerFieldUpdater.newUpdater
        (AtomicIntegerFieldUpdaterTest.class, "protectedField");
    this.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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkPackageAccess(AtomicIntegerFieldUpdaterTest obj) {
    obj.x = 72;
    AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
        AtomicIntegerFieldUpdater.newUpdater
        (AtomicIntegerFieldUpdaterTest.class, "x");
    assertEquals(72, a.get(obj));
    assertTrue(a.compareAndSet(obj, 72, 73));
    assertEquals(73, a.get(obj));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
public void checkPrivateAccess(AtomicIntegerFieldUpdaterTest obj) {
    try {
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
            AtomicIntegerFieldUpdater.newUpdater
            (AtomicIntegerFieldUpdaterTest.class, "privateField");
        throw new AssertionError("should throw");
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}
项目:openjdk9    文件:AtomicIntegerFieldUpdaterTest.java   
/**
 * 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));
}