code: protected void dispatchQueuedEvents() {
      // don't dispatch if we're already dispatching, that would allow reentrancy and out-of-order events. Instead, leave
      // the events to be dispatched after the in-progress dispatch is complete.
      if (isDispatching.get()) {
        return;
      }
  
      isDispatching.set(true);
      try {
        while (true) {
          EventWithHandler eventWithHandler = eventsToDispatch.get().poll();
          if (eventWithHandler == null) {
            break;
          }
  
          if (eventWithHandler.handler.isValid()) {
            dispatch(eventWithHandler.event, eventWithHandler.handler);
          }
        }
      } finally {
        isDispatching.set(false);
      }
    }
docs: Drain the queue of events to be dispatched. As the queue is being drained, new events may be posted to the end of
  the queue.