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

The following examples show how to use java.util.concurrent.atomic.AtomicLongFieldUpdater#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: 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 2
Source File: AtomicLongFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkPrivateAccess() {
    try {
        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
            AtomicLongFieldUpdater.newUpdater
            (AtomicLongFieldUpdaterTest.class, "privateField");
        shouldThrow();
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
 
Example 3
Source File: AtomicLongFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkCompareAndSetProtectedSub() {
    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
        AtomicLongFieldUpdater.newUpdater
        (AtomicLongFieldUpdaterTest.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));
}
 
Example 4
Source File: AtomicLongFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkPackageAccess(AtomicLongFieldUpdaterTest obj) {
    obj.x = 72L;
    AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
        AtomicLongFieldUpdater.newUpdater
        (AtomicLongFieldUpdaterTest.class, "x");
    assertEquals(72L, a.get(obj));
    assertTrue(a.compareAndSet(obj, 72L, 73L));
    assertEquals(73L, a.get(obj));
}
 
Example 5
Source File: AtomicLongFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkPrivateAccess(AtomicLongFieldUpdaterTest obj) {
    try {
        AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a =
            AtomicLongFieldUpdater.newUpdater
            (AtomicLongFieldUpdaterTest.class, "privateField");
        throw new AssertionError("should throw");
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
 
Example 6
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 7
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 long field.
 */
public static <T> AtomicLongFieldUpdater<T> newLongFieldUpdater(
    Class<T> tclass, String fieldName) {
  if (hasLongCAS && UnsafeHolder.hasUnsafe()) {
    return new UnsafeAtomicLongFieldUpdater<T>(tclass, fieldName);
  }
  else {
    return AtomicLongFieldUpdater.newUpdater(tclass, fieldName);
  }
}
 
Example 8
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 9
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 long field.
 */
public static <T> AtomicLongFieldUpdater<T> newLongFieldUpdater(
    Class<T> tclass, String fieldName) {
  if (hasLongCAS && UnsafeHolder.hasUnsafe()) {
    return new UnsafeAtomicLongFieldUpdater<T>(tclass, fieldName);
  }
  else {
    return AtomicLongFieldUpdater.newUpdater(tclass, fieldName);
  }
}
 
Example 10
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 11
Source File: AtomicLongFieldUpdaterTest.java    From openjdk-systemtest with Apache License 2.0 4 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 AtomicLongFieldUpdater class.
 */	
public void testAPI()
{
	// =================================================================================
	// Create instances of AtomicLongFieldUpdater to work with
	
	for(int i = 0; i < updaters.length; i++)
	{
		updaters[i] = AtomicLongFieldUpdater.newUpdater(AtomicTestObject.class, "volatileLong");
	}

	// =================================================================================
	// Constructors
	
	assertEquals("1 : get()", 42, 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 : addAndGet()", 43, getRandomUpdater().addAndGet(testObject1, 1));
	assertEquals("6 : get() addAndGet()", getRandomUpdater().get(testObject1), getRandomUpdater().addAndGet(testObject2, 1));
	assertEquals("7 : addAndGet()", 0, getRandomUpdater().addAndGet(testObject1, -43));
	assertEquals("8 : addAndGet() get()", 43, getRandomUpdater().addAndGet(testObject1, getRandomUpdater().get(testObject2)));
	assertEquals("9 : getAndAdd()", 43, getRandomUpdater().getAndAdd(testObject2, 43));
	assertEquals("10 : addAndGet()", 43, getRandomUpdater().addAndGet(testObject2, -43));
	assertEquals("11 : getAndAdd() addAndGet()", getRandomUpdater().getAndAdd(testObject2, 0), getRandomUpdater().addAndGet(testObject2, 0));
	assertEquals("12 : addAndGet() getAndAdd()", getRandomUpdater().addAndGet(testObject1, Long.MAX_VALUE), getRandomUpdater().getAndAdd(testObject2, 0) + Long.MAX_VALUE);
	assertEquals("13 : addAndGet() getAndAdd()", getRandomUpdater().addAndGet(testObject1, -Long.MAX_VALUE), getRandomUpdater().getAndAdd(testObject2, 0));
	
	assertEquals("14 : decrementAndGet()", 42, getRandomUpdater().decrementAndGet(testObject1));
	assertEquals("15 : get() decrementAndGet()", getRandomUpdater().get(testObject1), getRandomUpdater().decrementAndGet(testObject2));
	assertEquals("16 : getAndDecrement() incrementAndGet()", getRandomUpdater().getAndDecrement(testObject1), getRandomUpdater().incrementAndGet(testObject1));
	assertEquals("17 : decrementAndGet() getAndIncrement()", getRandomUpdater().decrementAndGet(testObject2), getRandomUpdater().getAndIncrement(testObject2));
	
	getRandomUpdater().set(testObject1, Long.MIN_VALUE);
	assertEquals("18 : set() get()", Long.MIN_VALUE, getRandomUpdater().get(testObject1));
	assertEquals("19 : decrementAndGet()", Long.MAX_VALUE, getRandomUpdater().decrementAndGet(testObject1));
	getRandomUpdater().set(testObject1, 42);
	assertEquals("20 : set() get()", 42, getRandomUpdater().get(testObject2));
	
	getRandomUpdater().set(testObject1, Long.MAX_VALUE);
	assertEquals("21 : set() get()", Long.MAX_VALUE, getRandomUpdater().get(testObject1));
	assertEquals("22 : incrementAndGet()", Long.MIN_VALUE, getRandomUpdater().incrementAndGet(testObject1));
	getRandomUpdater().set(testObject1, 42);
	assertEquals("23 : set() get()", 42, getRandomUpdater().get(testObject2));
	
	assertEquals("24 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject1, 42, Long.MAX_VALUE));
	assertEquals("25 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject1, Long.MAX_VALUE, 42));
	assertEquals("26 : compareAndSet()", false, getRandomUpdater().compareAndSet(testObject1, 0, 42));
	
	assertEquals("27 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject2, 42, Long.MIN_VALUE));
	assertEquals("28 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject2, Long.MIN_VALUE, 42));
	assertEquals("29 : compareAndSet()", false, getRandomUpdater().compareAndSet(testObject2, 0, 42));

	assertEquals("30 : get() get()", getRandomUpdater().get(testObject1) == 42, getRandomUpdater().get(testObject2) == 42);
	assertEquals("31 : compareAndSet() get() incrementAndGet()", true, getRandomUpdater().compareAndSet(testObject1, getRandomUpdater().get(testObject1), getRandomUpdater().incrementAndGet(testObject2)));
	assertEquals("32 : get() get()", getRandomUpdater().get(testObject1), getRandomUpdater().get(testObject2));
	assertEquals("33 : getAndDecrement() incrementAndGet()", getRandomUpdater().getAndDecrement(testObject1), getRandomUpdater().incrementAndGet(testObject1));
	assertEquals("34 : andAndGet() get() addAndGet() get()", getRandomUpdater().addAndGet(testObject1, getRandomUpdater().get(testObject1)), getRandomUpdater().addAndGet(testObject2, getRandomUpdater().get(testObject2)));
	assertEquals("35 : compareAndSet() get()", true, getRandomUpdater().compareAndSet(testObject1, getRandomUpdater().get(testObject1), 42));
	assertEquals("36 : compareAndSet() get()", true, getRandomUpdater().compareAndSet(testObject2, getRandomUpdater().get(testObject2), 42));
}
 
Example 12
Source File: AtomicLongFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
    return AtomicLongFieldUpdater.newUpdater
        (AtomicLongFieldUpdaterTest.class, fieldName);
}
 
Example 13
Source File: Atomic8Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
AtomicLongFieldUpdater<Atomic8Test> aLongFieldUpdater() {
    return AtomicLongFieldUpdater.newUpdater
        (Atomic8Test.class, "aLongField");
}
 
Example 14
Source File: AtomicLongFieldUpdaterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> updaterFor(String fieldName) {
    return AtomicLongFieldUpdater.newUpdater
        (AtomicLongFieldUpdaterTest.class, fieldName);
}
 
Example 15
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
AtomicLongFieldUpdater aLongFieldUpdater() {
    return AtomicLongFieldUpdater.newUpdater
        (Atomic8Test.class, "aLongField");
}
 
Example 16
Source File: Reflect.java    From bazel with Apache License 2.0 4 votes vote down vote up
void keep7() throws SecurityException {
  AtomicLongFieldUpdater.newUpdater(Reflect2.class, "fieldLong");
  AtomicLongFieldUpdater.newUpdater(Reflect2.class, "fieldLong2");
}