private void flush ( ) { try { QueuedCommand cmd ; int i = 0 ; boolean flushedOnce = false ; while ( ( cmd = queue . poll ( ) ) != null ) { cmd . run ( channel ) ; if ( ++ i == DEQUE_CHUNK_SIZE ) { i = 0 ; // Flush each chunk so we are releasing buffers periodically. In theory this loop // might never end as new events are continuously added to the queue, if we never // flushed in that case we would be guaranteed to OOM. channel . flush ( ) ; flushedOnce = true ; } } // Must flush at least once, even if there were no writes. if ( i != 0 || ! flushedOnce ) { channel . flush ( ) ; } } finally { // Mark the write as done, if the queue is non-empty after marking trigger a new write. scheduled . set ( false ) ; if ( ! queue . isEmpty ( ) ) { scheduleFlush ( ) ; } } }
Process the queue of commands and dispatch them to the stream . This method is only called in the event loop