本文整理汇总了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"));
}
示例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());
}
}
示例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));
}
示例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));
}
示例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));
}
示例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();
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例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));
}
示例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");
}
示例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;
}
}
示例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);
}
示例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);
}
示例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");
}