Java Code Examples for java.util.concurrent.atomic.AtomicReference.getAndSet()

The following are Jave code examples for showing how to use getAndSet() of the java.util.concurrent.atomic.AtomicReference class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: common-services   File: ServiceMessageHandlerTest.java   Source Code and License Vote up 7 votes
private ResultSet<String> createBlockingResultSet(BlockingQueue<String> queue, Supplier<Boolean> finished) {
  AtomicReference<String> ref = new AtomicReference<>();
  AtomicInteger sizeref = new AtomicInteger(0);
  Iterator<String> blockingIterator = new Iterator<String>() {
    @Override
    public boolean hasNext() {
      if (ref.get() != null) return true;
      while (!finished.get()) {
        if (ref.get() != null) return true;
        try {
          ref.set(queue.poll(1000, TimeUnit.MILLISECONDS));
        } catch (InterruptedException e) {
          throw new RuntimeException(e);
        }
      }
      return ref.get() != null;
    }

    @Override
    public String next() {
      sizeref.incrementAndGet();
      return ref.getAndSet(null);
    }
  };
  return createResultSet(blockingIterator);
}
 
Example 2
Project: RxJava3-preview   File: ExceptionHelper.java   Source Code and License Vote up 5 votes
public static <T> Throwable terminate(AtomicReference<Throwable> field) {
    Throwable current = field.get();
    if (current != TERMINATED) {
        current = field.getAndSet(TERMINATED);
    }
    return current;
}
 
Example 3
Project: Reactive4JavaFlow   File: ExceptionHelper.java   Source Code and License Vote up 5 votes
public static Throwable terminate(AtomicReference<Throwable> errors) {
    Throwable a = (Throwable)errors.getAcquire();
    if (a != TERMINATED) {
        a = (Throwable)errors.getAndSet(TERMINATED);
    }
    return a;
}
 
Example 4
Project: Reactive4JavaFlow   File: VarHandleCostPerf.java   Source Code and License Vote up 5 votes
public static boolean cancel(AtomicReference<Flow.Subscription> field) {
    Flow.Subscription a = field.getAcquire();
    if (a != CANCELLED) {
        a = field.getAndSet(CANCELLED);
        if (a != CANCELLED) {
            if (a != null) {
                a.cancel();
            }
            return true;
        }
    }
    return false;
}