Class QueuedSocketWriter

  • All Implemented Interfaces:
    java.lang.Runnable

    class QueuedSocketWriter
    extends java.lang.Thread
    • Nested Class Summary

      • Nested classes/interfaces inherited from class java.lang.Thread

        java.lang.Thread.State, java.lang.Thread.UncaughtExceptionHandler
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private boolean done  
      private boolean flushed
      Set to true when flushing is completed.
      private java.lang.Object flushMutex
      Sending thread that requests the flushing, waits on this mutex until notified that the flush was completed (boolean 'flushed' below set to true).
      private int instanceId  
      private static int instanceIdCounter  
      (package private) static java.util.logging.Logger LOGGER  
      private static java.lang.String MSG_EXIT_LOOP  
      private static java.lang.String MSG_FLUSH_MSGS  
      (package private) java.io.PrintWriter out
      The actual writer object which will send printed data over the socket.
      private java.util.concurrent.LinkedBlockingQueue<java.lang.String> queue
      The actual queue holding all messages that need to be sent.
      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Constructor Description
      QueuedSocketWriter​(java.net.Socket socket)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void flushMessages()
      Enqueues a flush marker and waits on the mutex until the flushing of all messages enqueued prior to the marker have been sent.
      private java.lang.String readNextFromQueue()
      We use no timeout while waiting for next message in the queue.
      void run()  
      void sendMessage​(java.lang.String message)  
      void stopWriter()  
      • Methods inherited from class java.lang.Thread

        activeCount, checkAccess, clone, countStackFrames, currentThread, dumpStack, enumerate, getAllStackTraces, getContextClassLoader, getDefaultUncaughtExceptionHandler, getId, getName, getPriority, getStackTrace, getState, getThreadGroup, getUncaughtExceptionHandler, holdsLock, interrupt, interrupted, isAlive, isDaemon, isInterrupted, join, join, join, onSpinWait, resume, setContextClassLoader, setDaemon, setDefaultUncaughtExceptionHandler, setName, setPriority, setUncaughtExceptionHandler, sleep, sleep, start, stop, suspend, toString, yield
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • LOGGER

        static final java.util.logging.Logger LOGGER
      • queue

        private final java.util.concurrent.LinkedBlockingQueue<java.lang.String> queue
        The actual queue holding all messages that need to be sent. This is a concurrent-safe queue.
      • out

        java.io.PrintWriter out
        The actual writer object which will send printed data over the socket.
      • flushMutex

        private final java.lang.Object flushMutex
        Sending thread that requests the flushing, waits on this mutex until notified that the flush was completed (boolean 'flushed' below set to true).
      • flushed

        private boolean flushed
        Set to true when flushing is completed.
      • done

        private boolean done
      • instanceIdCounter

        private static int instanceIdCounter
      • instanceId

        private final int instanceId
    • Constructor Detail

      • QueuedSocketWriter

        public QueuedSocketWriter​(java.net.Socket socket)
                           throws java.io.IOException
        Throws:
        java.io.IOException
    • Method Detail

      • run

        public void run()
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • flushMessages

        public void flushMessages()
        Enqueues a flush marker and waits on the mutex until the flushing of all messages enqueued prior to the marker have been sent. (this does at the moment not imply that the client has received them (not to mention even has processed them).
      • stopWriter

        public void stopWriter()
      • sendMessage

        public void sendMessage​(java.lang.String message)
      • readNextFromQueue

        private java.lang.String readNextFromQueue()
        We use no timeout while waiting for next message in the queue. To get it out of the loop, we enqueue a special marker (MSG_EXIT_LOOP).
        Returns:
        String containing the next message to write.