Java Code Examples for java.util.concurrent.atomic.AtomicReferenceFieldUpdater#newUpdater()

The following examples show how to use java.util.concurrent.atomic.AtomicReferenceFieldUpdater#newUpdater() . 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: TestSolutionCompareAndSet.java    From java-katas with MIT License 5 votes vote down vote up
@Test
@Tag("PASSING")
@Order(2)
public void compareAndSetUsingAtomicReferenceFieldUpdater() {

    final AtomicReferenceFieldUpdater<TestSolutionCompareAndSet, Integer> valueUpdater =
            AtomicReferenceFieldUpdater.newUpdater(TestSolutionCompareAndSet.class,
                    Integer.class,
                    "privateVolatile");

    boolean exchanged = valueUpdater.compareAndSet(this, currentValue, newValue);

    assertTrue(exchanged,
            "The value should have been changed to 7, " +
                    "hence exchanged should be true"
            );

    assertEquals(newValue,
            valueUpdater.get(this),
            "The value of the privateVolatile should now be 7");

    exchanged = valueUpdater.compareAndSet(this, 2, 33);

    assertFalse(exchanged,
            "The value should not have changed since the expected value " +
                    "did not match, hence exchanged should be false"
            );

    assertEquals(newValue,
            valueUpdater.get(this),
            "The value of the privateVolatile should still be 7");
}
 
Example 2
Source File: AtomicFieldUpdaterUtil.java    From simple-netty-source with Apache License 2.0 5 votes vote down vote up
static <T, V> AtomicReferenceFieldUpdater<T, V> newRefUpdater(Class<T> tclass, Class<V> vclass, String fieldName) {
    if (AVAILABLE) {
        return AtomicReferenceFieldUpdater.newUpdater(tclass, vclass, fieldName);
    } else {
        return null;
    }
}
 
Example 3
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 4
Source File: AtomicUpdater.java    From Jupiter with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns an updater for objects with the given field.
 *
 * @param tClass    the class of the objects holding the field.
 * @param vClass    the class of the field
 * @param fieldName the name of the field to be updated.
 */
public static <U, W> AtomicReferenceFieldUpdater<U, W> newAtomicReferenceFieldUpdater(
        Class<U> tClass, Class<W> vClass, String fieldName) {
    try {
        if (UnsafeUtil.hasUnsafe()) {
            return new UnsafeAtomicReferenceFieldUpdater<>(UnsafeUtil.getUnsafeAccessor().getUnsafe(), tClass, fieldName);
        } else {
            return AtomicReferenceFieldUpdater.newUpdater(tClass, vClass, fieldName);
        }
    } catch (Throwable t) {
        return AtomicReferenceFieldUpdater.newUpdater(tClass, vClass, fieldName);
    }
}
 
Example 5
Source File: AtomicTest.java    From jtransc with Apache License 2.0 5 votes vote down vote up
static private void testAtomicReferenceFieldUpdater() {
	System.out.println("AtomicTest.testAtomicReferenceFieldUpdater:");
	AtomicTest obj = new AtomicTest();
	AtomicReferenceFieldUpdater<AtomicTest, Integer> updater = AtomicReferenceFieldUpdater.newUpdater(AtomicTest.class, Integer.class, "value");
	System.out.println(updater.get(obj));
	updater.set(obj, 5);
	System.out.println(updater.get(obj));
	updater.compareAndSet(obj, 4, -4);
	System.out.println(updater.get(obj));
	updater.compareAndSet(obj, 5, -5);
	System.out.println(updater.get(obj));
}
 
Example 6
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 reference field.
 */
public static <T, V> AtomicReferenceFieldUpdater<T, V> newReferenceFieldUpdater(
    Class<T> tclass, Class<V> vclass, String fieldName) {
  if (UnsafeHolder.hasUnsafe()) {
    return new UnsafeAtomicReferenceUpdater<T, V>(tclass, vclass, fieldName);
  }
  else {
    return AtomicReferenceFieldUpdater.newUpdater(tclass, vclass, fieldName);
  }
}
 
Example 7
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 8
Source File: AtomicReferenceFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkPrivateAccess(AtomicReferenceFieldUpdaterTest obj) {
    try {
        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
            AtomicReferenceFieldUpdater.newUpdater
            (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
        throw new AssertionError("should throw");
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
 
Example 9
Source File: AtomicReferenceFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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));
}
 
Example 10
Source File: AtomicReferenceFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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));
    assertFalse(seven == a.get(this));
    assertTrue(a.compareAndSet(this, m4, seven));
    assertSame(seven, a.get(this));
}
 
Example 11
Source File: AtomicReferenceFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkPrivateAccess() {
    try {
        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
            AtomicReferenceFieldUpdater.newUpdater
            (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
        shouldThrow();
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
 
Example 12
Source File: AtomicReferenceFieldUpdaterTest.java    From openjdk-systemtest with Apache License 2.0 5 votes vote down vote up
/**
 * 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"));		
}
 
Example 13
Source File: AtomicIntegerFieldUpdaterDemo.java    From JavaCommon with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    Person person = new Person("zhangsan", 11, 170);
    person.setHobby(new Hobby("打球", "足球,篮球"));
    AtomicIntegerFieldUpdater<Person> atomicIntegerFieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
    atomicIntegerFieldUpdater.addAndGet(person, 12);

    AtomicLongFieldUpdater<Person> atomicLongFieldUpdater = AtomicLongFieldUpdater.newUpdater(Person.class, "height");
    atomicLongFieldUpdater.addAndGet(person, 180);

    AtomicReferenceFieldUpdater<Person, Hobby> atomicReferenceFieldUpdater = AtomicReferenceFieldUpdater.newUpdater(Person.class, Hobby.class, "hobby");
    atomicReferenceFieldUpdater.getAndSet(person, new Hobby("打球", "排球,羽毛球"));

}
 
Example 14
Source File: TestKataCompareAndSet.java    From java-katas with MIT License 5 votes vote down vote up
@Test
@Tag("PASSING")
@Order(2)
public void compareAndSetUsingAtomicReferenceFieldUpdater() {

    final AtomicReferenceFieldUpdater<TestKataCompareAndSet, Integer> valueUpdater =
            AtomicReferenceFieldUpdater.newUpdater(TestKataCompareAndSet.class,
                    Integer.class,
                    "privateVolatile");

    boolean exchanged = valueUpdater.compareAndSet(this, currentValue, newValue);

    assertTrue(exchanged,
            "The value should have been changed to 7, " +
                    "hence exchanged should be true"
            );

    assertEquals(newValue,
            valueUpdater.get(this),
            "The value of the privateVolatile should now be 7");

    exchanged = valueUpdater.compareAndSet(this, 2, 33);

    assertFalse(exchanged,
            "The value should not have changed since the expected value " +
                    "did not match, hence exchanged should be false"
            );

    assertEquals(newValue,
            valueUpdater.get(this),
            "The value of the privateVolatile should still be 7");
}
 
Example 15
Source File: AtomicReferenceFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
    return AtomicReferenceFieldUpdater.newUpdater
        (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
}
 
Example 16
Source File: Atomic8Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() {
    return AtomicReferenceFieldUpdater.newUpdater
        (Atomic8Test.class, Integer.class, "anIntegerField");
}
 
Example 17
Source File: InMemorySessionManager.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private static AtomicReferenceFieldUpdater<SessionImpl, Object> createTokenUpdater() {
    return AtomicReferenceFieldUpdater.newUpdater(SessionImpl.class, Object.class, "evictionToken");
}
 
Example 18
Source File: InMemorySessionManager.java    From quarkus-http with Apache License 2.0 4 votes vote down vote up
private static AtomicReferenceFieldUpdater<SessionImpl, Object> createTokenUpdater() {
    return AtomicReferenceFieldUpdater.newUpdater(SessionImpl.class, Object.class, "evictionToken");
}
 
Example 19
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
AtomicReferenceFieldUpdater<Atomic8Test,Integer> anIntegerFieldUpdater() {
    return AtomicReferenceFieldUpdater.newUpdater
        (Atomic8Test.class, Integer.class, "anIntegerField");
}
 
Example 20
Source File: AtomicReferenceFieldUpdaterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
static AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest, Integer> updaterFor(String fieldName) {
    return AtomicReferenceFieldUpdater.newUpdater
        (AtomicReferenceFieldUpdaterTest.class, Integer.class, fieldName);
}