本文整理汇总了Java中java.util.concurrent.atomic.AtomicIntegerFieldUpdater.newUpdater方法的典型用法代码示例。如果您正苦于以下问题:Java AtomicIntegerFieldUpdater.newUpdater方法的具体用法?Java AtomicIntegerFieldUpdater.newUpdater怎么用?Java AtomicIntegerFieldUpdater.newUpdater使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.concurrent.atomic.AtomicIntegerFieldUpdater
的用法示例。
在下文中一共展示了AtomicIntegerFieldUpdater.newUpdater方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
@Test
public void test() throws InterruptedException {
AtomicIntegerFieldUpdater age = AtomicIntegerFieldUpdater.newUpdater(User.class, "age");
User user = new User();
ExecutorService executorService = Executors.newFixedThreadPool(10);
for (int i = 0; i < 100; i++) {
executorService.execute(() -> {
age.addAndGet(user, 1);
});
}
TimeUnit.MILLISECONDS.sleep(10);
System.out.println(user.getAge());
}
示例2: checkPrivateAccess
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
public void checkPrivateAccess() {
try {
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a =
AtomicIntegerFieldUpdater.newUpdater
(AtomicIntegerFieldUpdaterTest.class, "privateField");
shouldThrow();
} catch (RuntimeException success) {
assertNotNull(success.getCause());
}
}
示例3: checkCompareAndSetProtectedSub
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
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));
}
示例4: checkPackageAccess
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
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));
}
示例5: main
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的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();
}
示例6: SW_testCompareAtomicOps
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的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);
}
示例7: newIntegerFieldUpdater
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
/**
* 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);
}
}
示例8: reset
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
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 = 0;
this.minRequestTime = 0;
}
示例9: newIntUpdater
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
static <T> AtomicIntegerFieldUpdater<T> newIntUpdater(Class<T> tclass, String fieldName) {
if (AVAILABLE) {
return AtomicIntegerFieldUpdater.newUpdater(tclass, fieldName);
} else {
return null;
}
}
示例10: getAtomicHelper
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
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;
}
示例11: should_create_error_message_for_AtomicIntegerFieldUpdater
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
@Test
public void should_create_error_message_for_AtomicIntegerFieldUpdater() {
// GIVEN
AtomicIntegerFieldUpdater<Person> updater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
// WHEN
String message = shouldHaveValue(updater, 33, 20, joe).create(TEST_DESCRIPTION, CONFIGURATION_PROVIDER.representation());
// THEN
assertThat(message).isEqualTo(format("[TEST] %n" +
"Expecting <AtomicIntegerFieldUpdater> to have value:%n" +
" <20>%n" +
"but had:%n" +
" <33>%n" +
"to update target object:%n" +
" <Person [name=Joe, age=33, account=123456789]>"));
}
示例12: should_fail_if_expected_value_is_null_and_does_not_contain_expected_value
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
@Test
public void should_fail_if_expected_value_is_null_and_does_not_contain_expected_value() throws Exception {
AtomicIntegerFieldUpdater<Person> fieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
thrown.expectIllegalArgumentException("The expected value should not be <null>.");
assertThat(fieldUpdater).hasValue(null, person);
}
示例13: should_fail_if_atomicIntegerFieldUpdater_does_not_contain_expected_value
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
@Test
public void should_fail_if_atomicIntegerFieldUpdater_does_not_contain_expected_value() throws Exception {
AtomicIntegerFieldUpdater<Person> fieldUpdater = AtomicIntegerFieldUpdater.newUpdater(Person.class, "age");
thrown.expectAssertionError(shouldHaveValue(fieldUpdater, person.age, 25, person).create());
assertThat(fieldUpdater).hasValue(25, person);
}
示例14: keep6
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
void keep6() throws SecurityException {
AtomicIntegerFieldUpdater.newUpdater(Reflect2.class, "fieldPublic");
}
示例15: testAPI
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; //导入方法依赖的package包/类
/**
* 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));
}