Class AsyncServletOutputStreamWriter


  • final class AsyncServletOutputStreamWriter
    extends java.lang.Object
    Handles write actions from the container thread and the application thread.
    • Field Detail

      • writeState

        private final java.util.concurrent.atomic.AtomicReference<AsyncServletOutputStreamWriter.WriteState> writeState
        Memory boundary for write actions.
         WriteState curState = writeState.get();  // mark a boundary
         doSomething();  // do something within the boundary
         boolean successful = writeState.compareAndSet(curState, newState); // try to mark a boundary
         if (successful) {
           // state has not changed since
           return;
         } else {
           // state is changed by another thread while doSomething(), need recompute
         }
         

        There are two threads, the container thread (calling onWritePossible()) and the application thread (calling runOrBuffer()) that read and update the writeState. Only onWritePossible() may turn readyAndDrained from false to true, and only runOrBuffer() may turn it from true to false.

      • isReady

        private final java.util.function.BooleanSupplier isReady
      • writeChain

        private final java.util.Queue<AsyncServletOutputStreamWriter.ActionItem> writeChain
        New write actions will be buffered into this queue if the servlet output stream is not ready or the queue is not drained.
      • parkingThread

        @Nullable
        private volatile java.lang.Thread parkingThread
    • Method Detail

      • writeBytes

        void writeBytes​(byte[] bytes,
                        int numBytes)
                 throws java.io.IOException
        Called from application thread.
        Throws:
        java.io.IOException
      • flush

        void flush()
            throws java.io.IOException
        Called from application thread.
        Throws:
        java.io.IOException
      • complete

        void complete()
        Called from application thread.
      • onWritePossible

        void onWritePossible()
                      throws java.io.IOException
        Called from the container thread WriteListener.onWritePossible().
        Throws:
        java.io.IOException
      • assureReadyAndDrainedTurnsFalse

        private void assureReadyAndDrainedTurnsFalse()
      • runOrBuffer

        private void runOrBuffer​(AsyncServletOutputStreamWriter.ActionItem actionItem)
                          throws java.io.IOException
        Either execute the write action directly, or buffer the action and let the container thread drain it.

        Called from application thread.

        Throws:
        java.io.IOException