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

The following examples show how to use java.util.concurrent.atomic.AtomicIntegerFieldUpdater#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: 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 2
Source File: SerializingExecutor.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
private static AtomicHelper getAtomicHelper() {
  AtomicHelper helper;
  try {
    helper =
        new FieldUpdaterAtomicHelper(
            AtomicIntegerFieldUpdater.newUpdater(SerializingExecutor.class, "runState"));
  } catch (Throwable t) {
    log.log(Level.SEVERE, "FieldUpdaterAtomicHelper failed", t);
    helper = new SynchronizedAtomicHelper();
  }
  return helper;
}
 
Example 3
Source File: AtomicFieldUpdaterUtil.java    From simple-netty-source with Apache License 2.0 5 votes vote down vote up
static <T> AtomicIntegerFieldUpdater<T> newIntUpdater(Class<T> tclass, String fieldName) {
    if (AVAILABLE) {
        return AtomicIntegerFieldUpdater.newUpdater(tclass, fieldName);
    } else {
        return null;
    }
}
 
Example 4
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 5
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 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 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 8
Source File: SerializingExecutor.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
private static AtomicHelper getAtomicHelper() {
  AtomicHelper helper;
  try {
    helper =
        new FieldUpdaterAtomicHelper(
            AtomicIntegerFieldUpdater.newUpdater(SerializingExecutor.class, "runState"));
  } catch (Throwable t) {
    log.log(Level.SEVERE, "FieldUpdaterAtomicHelper failed", t);
    helper = new SynchronizedAtomicHelper();
  }
  return helper;
}
 
Example 9
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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());
    }
}
 
Example 10
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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));
}
 
Example 11
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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));
}
 
Example 12
Source File: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void checkPrivateAccess() {
    try {
        AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
            AtomicIntegerFieldUpdater.newUpdater
            (AtomicIntegerFieldUpdaterTest.class, "privateField");
        shouldThrow();
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
 
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: AtomicIntegerFieldUpdaterTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) {
    return AtomicIntegerFieldUpdater.newUpdater
        (AtomicIntegerFieldUpdaterTest.class, fieldName);
}
 
Example 15
Source File: Atomic8Test.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
AtomicIntegerFieldUpdater<Atomic8Test> anIntFieldUpdater() {
    return AtomicIntegerFieldUpdater.newUpdater
        (Atomic8Test.class, "anIntField");
}
 
Example 16
Source File: Atomic8Test.java    From j2objc with Apache License 2.0 4 votes vote down vote up
AtomicIntegerFieldUpdater anIntFieldUpdater() {
    return AtomicIntegerFieldUpdater.newUpdater
        (Atomic8Test.class, "anIntField");
}
 
Example 17
Source File: AtomicIntegerFieldUpdaterTest.java    From j2objc with Apache License 2.0 4 votes vote down vote up
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> updaterFor(String fieldName) {
    return AtomicIntegerFieldUpdater.newUpdater
        (AtomicIntegerFieldUpdaterTest.class, fieldName);
}
 
Example 18
Source File: AtomicIntegerFieldUpdaterTest.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 AtomicIntegerFieldUpdater class.
 */	
public void testAPI()
{
			
	// =================================================================================
	// Create instances of AtomicIntegerFieldUpdater to work with
	
	for(int i = 0; i < updaters.length; i++)
	{
		updaters[i] = AtomicIntegerFieldUpdater.newUpdater(AtomicTestObject.class, "volatileInt");
	}

	// =================================================================================
	// API Testing
	
	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, Integer.MAX_VALUE), getRandomUpdater().getAndAdd(testObject2, 0) + Integer.MAX_VALUE);
	assertEquals("13 : addAndGet() getAndAdd()", getRandomUpdater().addAndGet(testObject1, -Integer.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, Integer.MIN_VALUE);
	assertEquals("18 : set() get()", Integer.MIN_VALUE, getRandomUpdater().get(testObject1));
	assertEquals("19 : decrementAndGet()", Integer.MAX_VALUE, getRandomUpdater().decrementAndGet(testObject1));
	getRandomUpdater().set(testObject1, 42);
	assertEquals("20 : set() get()", 42, getRandomUpdater().get(testObject2));
	
	getRandomUpdater().set(testObject1, Integer.MAX_VALUE);
	assertEquals("21 : set() get()", Integer.MAX_VALUE, getRandomUpdater().get(testObject1));
	assertEquals("22 : incrementAndGet()", Integer.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, -2147483648));
	assertEquals("25 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject1, Integer.MIN_VALUE, 42));
	assertEquals("26 : compareAndSet()", false, getRandomUpdater().compareAndSet(testObject1, 0, 42));
	
	assertEquals("27 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject2, 42, 2147483647));
	assertEquals("28 : compareAndSet()", true, getRandomUpdater().compareAndSet(testObject2, Integer.MAX_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 19
Source File: Reflect.java    From bazel with Apache License 2.0 4 votes vote down vote up
void keep6() throws SecurityException {
  AtomicIntegerFieldUpdater.newUpdater(Reflect2.class, "fieldPublic");
}