本文整理汇总了Java中java.util.concurrent.atomic.AtomicStampedReference类的典型用法代码示例。如果您正苦于以下问题:Java AtomicStampedReference类的具体用法?Java AtomicStampedReference怎么用?Java AtomicStampedReference使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AtomicStampedReference类属于java.util.concurrent.atomic包,在下文中一共展示了AtomicStampedReference类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testGetSet
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
/**
* get returns the last values of reference and stamp set
*/
public void testGetSet() {
int[] mark = new int[1];
AtomicStampedReference ai = new AtomicStampedReference(one, 0);
assertSame(one, ai.getReference());
assertEquals(0, ai.getStamp());
assertSame(one, ai.get(mark));
assertEquals(0, mark[0]);
ai.set(two, 0);
assertSame(two, ai.getReference());
assertEquals(0, ai.getStamp());
assertSame(two, ai.get(mark));
assertEquals(0, mark[0]);
ai.set(one, 1);
assertSame(one, ai.getReference());
assertEquals(1, ai.getStamp());
assertSame(one, ai.get(mark));
assertEquals(1, mark[0]);
}
示例2: testCompareAndSet
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
/**
* compareAndSet succeeds in changing values if equal to expected reference
* and stamp else fails
*/
public void testCompareAndSet() {
int[] mark = new int[1];
AtomicStampedReference ai = new AtomicStampedReference(one, 0);
assertSame(one, ai.get(mark));
assertEquals(0, ai.getStamp());
assertEquals(0, mark[0]);
assertTrue(ai.compareAndSet(one, two, 0, 0));
assertSame(two, ai.get(mark));
assertEquals(0, mark[0]);
assertTrue(ai.compareAndSet(two, m3, 0, 1));
assertSame(m3, ai.get(mark));
assertEquals(1, mark[0]);
assertFalse(ai.compareAndSet(two, m3, 1, 1));
assertSame(m3, ai.get(mark));
assertEquals(1, mark[0]);
}
示例3: testCompareAndSetInMultipleThreads
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
/**
* compareAndSet in one thread enables another waiting for reference value
* to succeed
*/
public void testCompareAndSetInMultipleThreads() throws Exception {
final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!ai.compareAndSet(two, three, 0, 0))
Thread.yield();
}});
t.start();
assertTrue(ai.compareAndSet(one, two, 0, 0));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertSame(three, ai.getReference());
assertEquals(0, ai.getStamp());
}
示例4: testCompareAndSetInMultipleThreads2
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
/**
* compareAndSet in one thread enables another waiting for stamp value
* to succeed
*/
public void testCompareAndSetInMultipleThreads2() throws Exception {
final AtomicStampedReference ai = new AtomicStampedReference(one, 0);
Thread t = new Thread(new CheckedRunnable() {
public void realRun() {
while (!ai.compareAndSet(one, one, 1, 2))
Thread.yield();
}});
t.start();
assertTrue(ai.compareAndSet(one, one, 0, 1));
t.join(LONG_DELAY_MS);
assertFalse(t.isAlive());
assertSame(one, ai.getReference());
assertEquals(2, ai.getStamp());
}
示例5: testWeakCompareAndSet
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
/**
* repeated weakCompareAndSet succeeds in changing values when equal
* to expected
*/
public void testWeakCompareAndSet() {
int[] mark = new int[1];
AtomicStampedReference ai = new AtomicStampedReference(one, 0);
assertSame(one, ai.get(mark));
assertEquals(0, ai.getStamp());
assertEquals(0, mark[0]);
do {} while (!ai.weakCompareAndSet(one, two, 0, 0));
assertSame(two, ai.get(mark));
assertEquals(0, mark[0]);
do {} while (!ai.weakCompareAndSet(two, m3, 0, 1));
assertSame(m3, ai.get(mark));
assertEquals(1, mark[0]);
}
示例6: searchKey
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
private AtomicStampedReference<KAryBaseNode> searchKey(int key, KAryInternalNode l){
if(l.keys[0] == Integer.MAX_VALUE)
return l.children.get(0);
int left = 0;
int right = l.keyCount-1;
while(right > left){
int mid = (left+right)/2;
if( key < l.keys[mid]){
right = mid;
}
else{
left = mid+1;
}
}
if(left == l.keyCount-1 && (key >= l.keys[left]) )
return l.children.get(l.keyCount);
return l.children.get(left);
}
开发者ID:anandjbangad,项目名称:Non-Blocking-K--Ary-Search-Tree-Manual-Memory-Managgement,代码行数:19,代码来源:KArySearchTree.java
示例7: KAryInternalNode
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
public KAryInternalNode(int K, boolean root){
this.keys = new int[K-1];
this.keyCount = K-1;
for(int i = 0; i < keyCount; ++i){
this.keys[i] = Integer.MAX_VALUE;
}
if(root){
this.children = new ArrayList<AtomicStampedReference<KAryBaseNode>>(K);
this.children.add(new AtomicStampedReference<KAryBaseNode>(new KAryInternalNode(K,false),0 ) );
for(int i = 1; i < K; ++i ){
this.children.add(new AtomicStampedReference<KAryBaseNode>(new KAryLeafNode(K),0) );
}
}
else{
this.children = new ArrayList<AtomicStampedReference<KAryBaseNode>>(K);
for(int i = 0; i < K; ++i ){
this.children.add(new AtomicStampedReference<KAryBaseNode>(new KAryLeafNode(K),0));
}
}
}
开发者ID:anandjbangad,项目名称:Non-Blocking-K--Ary-Search-Tree-Manual-Memory-Managgement,代码行数:21,代码来源:KAryInternalNode.java
示例8: setRunning
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
public void setRunning() {
AtomicStampedReference<State> stateRef = state;
int newStamp = stamp.incrementAndGet();
int[] receiver = new int[1];
// Keep trying until stateRef is set with our stamp
for (;;) {
State was = stateRef.get(receiver);
if (was != State.STARTING) { // AS7-1103 only transition to running from STARTING
break;
}
synchronized (service) {
State newState = restartRequiredFlag ? State.RESTART_REQUIRED : State.RUNNING;
if (state.compareAndSet(was, newState, receiver[0], newStamp)) {
service.stateChanged(newState);
break;
}
}
}
}
示例9: setReloadRequired
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
public Object setReloadRequired() {
if (!reloadSupported) {
return setRestartRequired();
}
AtomicStampedReference<State> stateRef = state;
int newStamp = stamp.incrementAndGet();
int[] receiver = new int[1];
// Keep trying until stateRef is RELOAD_REQUIRED with our stamp
for (;;) {
State was = stateRef.get(receiver);
if (was == State.STARTING || was == State.STOPPING || was == State.RESTART_REQUIRED) {
break;
}
synchronized (service) {
if (stateRef.compareAndSet(was, State.RELOAD_REQUIRED, receiver[0], newStamp)) {
service.stateChanged(State.RELOAD_REQUIRED);
break;
}
}
}
return Integer.valueOf(newStamp);
}
示例10: setRestartRequired
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
public Object setRestartRequired() {
AtomicStampedReference<State> stateRef = state;
int newStamp = stamp.incrementAndGet();
int[] receiver = new int[1];
// Keep trying until stateRef is RESTART_REQUIRED with our stamp
for (;;) {
State was = stateRef.get(receiver);
if (was == State.STARTING || was == State.STOPPING) {
break;
}
synchronized (service) {
if (stateRef.compareAndSet(was, State.RESTART_REQUIRED, receiver[0], newStamp)) {
restartRequiredFlag = true;
service.stateChanged(State.RESTART_REQUIRED);
break;
}
}
}
return Integer.valueOf(newStamp);
}
示例11: status
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
@Override
public WorkloadStatusSnapshot status() throws MetricsCollectionException
{
if ( null != alreadyShutdownPolicy )
{
alreadyShutdownPolicy.apply();
}
AtomicStampedReference<WorkloadStatusSnapshot> statusSnapshotReference = eventHandler.statusSnapshot();
int oldStamp = statusSnapshotReference.getStamp();
ringBuffer.publishEvent( getWorkloadStatusTranslator );
while ( statusSnapshotReference.getStamp() <= oldStamp )
{
LockSupport.parkNanos( TimeUnit.MILLISECONDS.toNanos( 100 ) );
}
return statusSnapshotReference.getReference();
}
示例12: results
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
@Override
public WorkloadResultsSnapshot results() throws MetricsCollectionException
{
if ( null != alreadyShutdownPolicy )
{
alreadyShutdownPolicy.apply();
}
AtomicStampedReference<WorkloadResultsSnapshot> resultsSnapshotReference = eventHandler.resultsSnapshot();
int oldStamp = resultsSnapshotReference.getStamp();
ringBuffer.publishEvent( getWorkloadResultsTranslator );
while ( resultsSnapshotReference.getStamp() <= oldStamp )
{
LockSupport.parkNanos( TimeUnit.MILLISECONDS.toNanos( 100 ) );
}
return resultsSnapshotReference.getReference();
}
示例13: should_create_error_message_for_AtomicStampedReference
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
@Test
public void should_create_error_message_for_AtomicStampedReference() throws Exception {
// GIVEN
AtomicStampedReference<String> actual = new AtomicStampedReference<>("foo", 123);
// WHEN
String message = shouldHaveReference(actual, actual.getReference(), "bar").create(TEST_DESCRIPTION,
CONFIGURATION_PROVIDER.representation());
// THEN
assertThat(message).isEqualTo(format("[TEST] %n" +
"Expecting%n" +
" <AtomicStampedReference[stamp=123, reference=\"foo\"]>%n" +
"to have reference:%n" +
" <\"bar\">%n" +
"but had:%n" +
" <\"foo\">"));
}
示例14: testConstructor
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
/**
* constructor initializes to given reference and stamp
*/
public void testConstructor() {
AtomicStampedReference ai = new AtomicStampedReference(one, 0);
assertSame(one, ai.getReference());
assertEquals(0, ai.getStamp());
AtomicStampedReference a2 = new AtomicStampedReference(null, 1);
assertNull(a2.getReference());
assertEquals(1, a2.getStamp());
}
示例15: testAttemptStamp
import java.util.concurrent.atomic.AtomicStampedReference; //导入依赖的package包/类
/**
* attemptStamp succeeds in single thread
*/
public void testAttemptStamp() {
int[] mark = new int[1];
AtomicStampedReference ai = new AtomicStampedReference(one, 0);
assertEquals(0, ai.getStamp());
assertTrue(ai.attemptStamp(one, 1));
assertEquals(1, ai.getStamp());
assertSame(one, ai.get(mark));
assertEquals(1, mark[0]);
}