Code example for AtomicReference

Methods: compareAndSet

0
 */ 
class ReceiverState { 
  /** 
   * Stores the Event that is next in line to be dispatched to the ReceiverTarget. 
   */ 
  private final AtomicReference<Event> next = new AtomicReference<Event>();
  /** 
   * The remaining Events, exclusive of {@link #next}, to be dispatched to the 
   * {@link ReceiverTarget}. 
   */ 
  private final Queue<Event> toDispatch;
 
  public ReceiverState(Collection<Event> toDispatch) {
    this.toDispatch = new ConcurrentLinkedQueue<Event>(toDispatch);
    getNext(); 
  } 
 
  /** 
   * Returns the Event that should be fired next. 
   */ 
  public Event getNext() { 
    Event toReturn = toDispatch.poll();
    if (!next.compareAndSet(null, toReturn)) {
      throw new IllegalStateException("Calling getNext() without having staged the previous event");
    } 
    return toReturn;
  } 
 
Contextual code suggestions in your IDE  Get Codota for Java