當前位置: 首頁>>代碼示例>>Java>>正文


Java AtomicReferenceFieldUpdater.newUpdater方法代碼示例

本文整理匯總了Java中java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater方法的典型用法代碼示例。如果您正苦於以下問題:Java AtomicReferenceFieldUpdater.newUpdater方法的具體用法?Java AtomicReferenceFieldUpdater.newUpdater怎麽用?Java AtomicReferenceFieldUpdater.newUpdater使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.util.concurrent.atomic.AtomicReferenceFieldUpdater的用法示例。


在下文中一共展示了AtomicReferenceFieldUpdater.newUpdater方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: testAPI

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
/**
 * 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"));		
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-systemtest,代碼行數:33,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例2: checkPrivateAccess

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
public void checkPrivateAccess() {
    try {
        AtomicReferenceFieldUpdater<AtomicReferenceFieldUpdaterTest,Integer> a =
            AtomicReferenceFieldUpdater.newUpdater
            (AtomicReferenceFieldUpdaterTest.class, Integer.class, "privateField");
        shouldThrow();
    } catch (RuntimeException success) {
        assertNotNull(success.getCause());
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:11,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例3: checkCompareAndSetProtectedSub

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
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));
    assertNotSame(seven, a.get(this));
    assertTrue(a.compareAndSet(this, m4, seven));
    assertSame(seven, a.get(this));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例4: checkPackageAccess

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
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));
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:10,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例5: checkCompareAndSetProtectedSub

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
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));
}
 
開發者ID:campolake,項目名稱:openjdk9,代碼行數:14,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例6: main

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
public static void main(String[] args) throws InterruptedException {
	final AtomicReferenceFieldUpdater<Person, String> nameFieldUpdater = 
			AtomicReferenceFieldUpdater.newUpdater(Person.class, String.class, "name");
	final AtomicIntegerFieldUpdater<Person> ageFieldUpdater = 
			AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
	
	final Person person = new Person(1, "zhangsan", 20);
	final Random radom = new Random();
	final CountDownLatch latch = new CountDownLatch(SIZE);
	for(int i = 0; i < SIZE; i++) {
		new Thread(new Runnable() {
			public void run() {
				try {
					TimeUnit.MICROSECONDS.sleep(radom.nextInt(1000));
					if(nameFieldUpdater.compareAndSet(person, "zhangsan", "lisi")) {
						System.out.println(Thread.currentThread().getName() + " update field name success.");
					}
					
					ThreadUtils.sleepSilently(radom.nextInt(1000));
					if(ageFieldUpdater.compareAndSet(person, 20, 30)) {
						System.out.println(Thread.currentThread().getName() + " update field age success.");
					}
				} catch(Exception e) {
					e.printStackTrace();
				} finally {
					latch.countDown();
				}
			}
		}, "thread"+i).start();
	}
	latch.await();
}
 
開發者ID:walle-liao,項目名稱:jaf-examples,代碼行數:33,代碼來源:AtomicReferenceFieldUpdaterTest.java

示例7: SW_testCompareAtomicOps

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
/** 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);
}
 
開發者ID:gemxd,項目名稱:gemfirexd-oss,代碼行數:37,代碼來源:ConcurrentMapOpsTest.java

示例8: newReferenceFieldUpdater

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
/**
 * 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);
  }
}
 
開發者ID:gemxd,項目名稱:gemfirexd-oss,代碼行數:13,代碼來源:AtomicUpdaterFactory.java

示例9: DependencyManager

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
/**
 * Creates a new DependencyManager
 * @param script The script to manage dependencies for
 * @param type The script type
 */
public DependencyManager(final T script, final Class<T> type) {
	this.script = script;
	scriptName = this.script.getClass().getName();
	log = LogManager.getLogger(type);

	cache = GlobalCacheService.getInstance();
	try {
		for(Field f: type.getDeclaredFields()) {
			final Dependency d = f.getAnnotation(Dependency.class);
			if(d!=null) {
				final String cacheKey = d.value().trim();
				depFields.put(cacheKey, f);
				depDefs.put(cacheKey, d);
				f.setAccessible(true);
				final AtomicReferenceFieldUpdater<T, Object> updater =  AtomicReferenceFieldUpdater.newUpdater(type, Object.class, f.getName());
				depUpdaters.put(cacheKey, updater);
				final Object o = cache.getOrNotify(cacheKey, d.type(), this, d.timeout(), d.unit());
				if(o==null) {
					script.addPendingDependency(cacheKey);
				} else {
					log.info("Seting dependent value [{}] on [{}] from cache entry [{}]", o.getClass().getName(), scriptName, cacheKey);
					updater.set(script, o);
					//PrivateAccessor.setFieldValue(script, f.getName(), o);
					cache.addCacheEventListener(this, cacheKey);
					log.info("Dependent value [{}] initialized on [{}] from cache entry [{}]", o.getClass().getName(), scriptName, cacheKey);
				}
			}
		}
	} catch (Exception ex) {
		throw new RuntimeException("Failed to scan script [" + script.sourceReader + "] for dependencies", ex);
	}
}
 
開發者ID:nickman,項目名稱:HeliosStreams,代碼行數:38,代碼來源:DependencyManager.java

示例10: testAtomicReferenceFieldUpdater

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
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));
}
 
開發者ID:jtransc,項目名稱:jtransc,代碼行數:13,代碼來源:AtomicTest.java

示例11: FieldUpdater

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
public FieldUpdater(V value, Function<V, V> updater, TimeProperty updateInterval) {
    this.value = value;
    this.updater = updater;
    this.updateInterval = new AtomicLong(updateInterval.to(TimeUnit.MILLISECONDS));
    lastSet = new AtomicLong(System.currentTimeMillis());
    valueUpdater = AtomicReferenceFieldUpdater.newUpdater(FieldUpdater.class, Object.class, "value");
}
 
開發者ID:ahmadmo,項目名稱:cbox,代碼行數:8,代碼來源:FieldUpdater.java

示例12: newRefUpdater

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
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;
    }
}
 
開發者ID:nyankosama,項目名稱:simple-netty-source,代碼行數:8,代碼來源:AtomicFieldUpdaterUtil.java

示例13: should_fail_if_expected_value_is_null_and_does_not_contain_expected_value

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
@Test
public void should_fail_if_expected_value_is_null_and_does_not_contain_expected_value() throws Exception {
  AtomicReferenceFieldUpdater<Person,String> fieldUpdater = AtomicReferenceFieldUpdater.newUpdater(Person.class, String.class, "name");
  fieldUpdater.set(person, "Frodo");
  thrown.expectAssertionError(shouldHaveValue(fieldUpdater, person.name, null, person).create());

  assertThat(fieldUpdater).hasValue(null, person);
}
 
開發者ID:joel-costigliola,項目名稱:assertj-core,代碼行數:9,代碼來源:AtomicReferenceFieldUpdater_hasValue_Test.java

示例14: should_fail_if_atomicReferenceFieldUpdater_does_not_contain_expected_value

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
@Test
public void should_fail_if_atomicReferenceFieldUpdater_does_not_contain_expected_value() throws Exception {
  AtomicReferenceFieldUpdater<Person,String> fieldUpdater = AtomicReferenceFieldUpdater.newUpdater(Person.class, String.class, "name");

  thrown.expectAssertionError(shouldHaveValue(fieldUpdater, person.name, "Frodo", person).create());

  assertThat(fieldUpdater).hasValue("Frodo", person);
}
 
開發者ID:joel-costigliola,項目名稱:assertj-core,代碼行數:9,代碼來源:AtomicReferenceFieldUpdater_hasValue_Test.java

示例15: keep8

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; //導入方法依賴的package包/類
void keep8() throws SecurityException {
  AtomicReferenceFieldUpdater.newUpdater(Reflect2.class, Reflect2.A.class, "a");
  AtomicReferenceFieldUpdater.newUpdater(Reflect2.class, Reflect2.A.class, "b");
  AtomicReferenceFieldUpdater.newUpdater(Reflect2.class, Object.class, "c");
}
 
開發者ID:inferjay,項目名稱:r8,代碼行數:6,代碼來源:Reflect.java


注:本文中的java.util.concurrent.atomic.AtomicReferenceFieldUpdater.newUpdater方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。