java.util.concurrent.atomic.AtomicReference.get()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(8.6k)|赞(0)|评价(0)|浏览(135)

本文整理了Java中java.util.concurrent.atomic.AtomicReference.get()方法的一些代码示例,展示了AtomicReference.get()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。AtomicReference.get()方法的具体详情如下:
包路径:java.util.concurrent.atomic.AtomicReference
类名称:AtomicReference
方法名:get

AtomicReference.get介绍

[英]Gets the current value.
[中]获取当前值。

代码示例

代码示例来源:origin: ReactiveX/RxJava

void doTerminate() {
  Runnable r = onTerminate.get();
  if (r != null && onTerminate.compareAndSet(r, null)) {
    r.run();
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Override
public void dispose() {
  if (subscribers.get() != TERMINATED) {
    @SuppressWarnings("unchecked")
    InnerSubscriber[] ps = subscribers.getAndSet(TERMINATED);
    if (ps != TERMINATED) {
      current.compareAndSet(PublishSubscriber.this, null);
      SubscriptionHelper.cancel(upstream);
    }
  }
}

代码示例来源:origin: ctripcorp/apollo

@Override
public String getContent() {
 if (m_contentCache.get() == null) {
  m_contentCache.set(doGetContent());
 }
 return m_contentCache.get();
}

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void receiptNotReceived() {
  TaskScheduler taskScheduler = mock(TaskScheduler.class);
  this.session.afterConnected(this.connection);
  this.session.setTaskScheduler(taskScheduler);
  AtomicReference<Boolean> notReceived = new AtomicReference<>();
  ScheduledFuture future = mock(ScheduledFuture.class);
  when(taskScheduler.schedule(any(Runnable.class), any(Date.class))).thenReturn(future);
  StompHeaders headers = new StompHeaders();
  headers.setDestination("/topic/foo");
  headers.setReceipt("my-receipt");
  Receiptable receiptable = this.session.send(headers, "payload");
  receiptable.addReceiptLostTask(() -> notReceived.set(true));
  ArgumentCaptor<Runnable> taskCaptor = ArgumentCaptor.forClass(Runnable.class);
  verify(taskScheduler).schedule(taskCaptor.capture(), (Date) notNull());
  Runnable scheduledTask = taskCaptor.getValue();
  assertNotNull(scheduledTask);
  assertNull(notReceived.get());
  scheduledTask.run();
  assertTrue(notReceived.get());
  verify(future).cancel(true);
  verifyNoMoreInteractions(future);
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testExecuteBlockingTTCL() throws Exception {
 ClassLoader cl = Thread.currentThread().getContextClassLoader();
 assertNotNull(cl);
 CountDownLatch latch = new CountDownLatch(1);
 AtomicReference<ClassLoader> blockingTCCL = new AtomicReference<>();
 vertx.<String>executeBlocking(future -> {
  future.complete("whatever");
  blockingTCCL.set(Thread.currentThread().getContextClassLoader());
 }, ar -> {
  assertTrue(ar.succeeded());
  assertEquals("whatever", ar.result());
  latch.countDown();
 });
 assertSame(cl, Thread.currentThread().getContextClassLoader());
 awaitLatch(latch);
 assertSame(cl, blockingTCCL.get());
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Override
public void onBinaryFrame(byte[] frame, boolean finalFragment, int rsv) {
 if (text.get() == null) {
  text.set(frame);
 } else {
  byte[] n = new byte[text.get().length + frame.length];
  System.arraycopy(text.get(), 0, n, 0, text.get().length);
  System.arraycopy(frame, 0, n, text.get().length, frame.length);
  text.set(n);
 }
 latch.countDown();
}

代码示例来源:origin: stackoverflow.com

AtomicReference<Object> cache = new AtomicReference<Object>();

Object cachedValue = new Object();
cache.set(cachedValue);

//... time passes ...
Object cachedValueToUpdate = cache.get();
//... do some work to transform cachedValueToUpdate into a new version
Object newValue = someFunctionOfOld(cachedValueToUpdate);
boolean success = cache.compareAndSet(cachedValue,cachedValueToUpdate);

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testFactory() throws Exception {
 AtomicReference<Thread> metricsThread = new AtomicReference<>();
 AtomicReference<Context> metricsContext = new AtomicReference<>();
 VertxMetricsFactory factory = (options) -> {
  metricsThread.set(Thread.currentThread());
  metricsContext.set(Vertx.currentContext());
  return DummyVertxMetrics.INSTANCE;
 };
 vertx(new VertxOptions().setMetricsOptions(new MetricsOptions().setEnabled(true).setFactory(factory)));
 assertSame(Thread.currentThread(), metricsThread.get());
 assertNull(metricsContext.get());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
protected void hookOnError(Throwable throwable) {
  this.error.set(throwable);
  if (this.dataBuffer.get() == null) {
    this.sink.error(throwable);
  }
}

代码示例来源:origin: ReactiveX/RxJava

void setFirst(Future<?> f) {
  for (;;) {
    Future<?> current = first.get();
    if (current == CANCELLED) {
      f.cancel(runner != Thread.currentThread());
      return;
    }
    if (first.compareAndSet(current, f)) {
      return;
    }
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
 public void testCallbackInvokedOnFailure() throws Exception {

  // will trigger java.net.UnknownHostException
  String hostName = "zoom.zoom.zen.tld";

  VertxOptions options = new VertxOptions()
   .setClusterManager(new FakeClusterManager())
   .setClusterHost(hostName);

  AtomicReference<AsyncResult<Vertx>> resultRef = new AtomicReference<>();

  CountDownLatch latch = new CountDownLatch(1);
  Vertx.clusteredVertx(options, ar -> {
   resultRef.set(ar);
   latch.countDown();
  });
  awaitLatch(latch);

  assertFalse(resultRef.get() == null);
  assertTrue(resultRef.get().failed());
  assertTrue("Was expecting failure to be an instance of UnknownHostException", resultRef.get().cause() instanceof UnknownHostException);
 }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Override
public void onBinaryFrame(byte[] frame, boolean finalFragment, int rsv) {
 if (text.get() == null) {
  text.set(frame);
 } else {
  byte[] n = new byte[text.get().length + frame.length];
  System.arraycopy(text.get(), 0, n, 0, text.get().length);
  System.arraycopy(frame, 0, n, text.get().length, frame.length);
  text.set(n);
 }
 latch.countDown();
}

代码示例来源:origin: org.apache.commons/commons-lang3

/**
 * Get (and initialize, if not initialized yet) the required object
 *
 * @return lazily initialized object
 * @throws ConcurrentException if the initialization of the object causes an
 * exception
 */
@Override
public final T get() throws ConcurrentException {
  T result;
  while ((result = reference.get()) == null) {
    if (factory.compareAndSet(null, this)) {
      reference.set(initialize());
    }
  }
  return result;
}

代码示例来源:origin: eclipse-vertx/vert.x

@Test
public void testCloseWorkerPool() throws Exception {
 String poolName = "vert.x-" + TestUtils.randomAlphaString(10);
 AtomicReference<Thread> thread = new AtomicReference<>();
 WorkerExecutor worker1 = vertx.createSharedWorkerExecutor(poolName);
 WorkerExecutor worker2 = vertx.createSharedWorkerExecutor(poolName);
 worker1.executeBlocking(fut -> {
  thread.set(Thread.currentThread());
 }, ar -> {
 });
 assertWaitUntil(() -> thread.get() != null);
 worker1.close();
 assertNotSame(thread.get().getState(), Thread.State.TERMINATED);
 worker2.close();
 assertWaitUntil(() -> thread.get().getState() == Thread.State.TERMINATED);
}

代码示例来源:origin: spring-projects/spring-framework

private void sinkDataBuffer() {
    DataBuffer dataBuffer = this.dataBuffer.get();
    Assert.state(dataBuffer != null, "DataBuffer should not be null");
    this.sink.next(dataBuffer);
    this.dataBuffer.set(null);
  }
}

代码示例来源:origin: ReactiveX/RxJava

void setRest(Future<?> f) {
    for (;;) {
      Future<?> current = rest.get();
      if (current == CANCELLED) {
        f.cancel(runner != Thread.currentThread());
        return;
      }
      if (rest.compareAndSet(current, f)) {
        return;
      }
    }
  }
}

代码示例来源:origin: AsyncHttpClient/async-http-client

@Override
public void onBinaryFrame(byte[] frame, boolean finalFragment, int rsv) {
 if (text.get() == null) {
  text.set(frame);
 } else {
  byte[] n = new byte[text.get().length + frame.length];
  System.arraycopy(text.get(), 0, n, 0, text.get().length);
  System.arraycopy(frame, 0, n, text.get().length, frame.length);
  text.set(n);
 }
 latch.countDown();
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void subscribeActionReportsUnsubscribedAfter() {
  PublishSubject<String> stringSubject = PublishSubject.create();
  Completable completable = stringSubject.ignoreElements();
  final AtomicReference<Disposable> disposableRef = new AtomicReference<Disposable>();
  Disposable completableSubscription = completable.subscribe(new Action() {
    @Override
    public void run() {
      if (disposableRef.get().isDisposed()) {
        disposableRef.set(null);
      }
    }
  });
  disposableRef.set(completableSubscription);
  stringSubject.onComplete();
  assertTrue("Not unsubscribed?", completableSubscription.isDisposed());
  assertNotNull("Unsubscribed before the call to onComplete", disposableRef.get());
}

代码示例来源:origin: spring-projects/spring-framework

@Override
public Mono<Void> invalidate() {
  this.state.set(State.EXPIRED);
  getAttributes().clear();
  InMemoryWebSessionStore.this.sessions.remove(this.id.get());
  return Mono.empty();
}

代码示例来源:origin: ReactiveX/RxJava

boolean add(CompletableDisposable inner) {
  for (;;) {
    CompletableDisposable[] a = observers.get();
    if (a == TERMINATED) {
      return false;
    }
    int n = a.length;
    CompletableDisposable[] b = new CompletableDisposable[n + 1];
    System.arraycopy(a, 0, b, 0, n);
    b[n] = inner;
    if (observers.compareAndSet(a, b)) {
      return true;
    }
  }
}

相关文章