Package editor.util

Class TaskQueue

  • All Implemented Interfaces:
    java.lang.Runnable

    public class TaskQueue
    extends java.lang.Thread
    A general purpose concurrent task queue. Facilitates creating named queues containing any number of Runnable tasks. A task is posted to its queue asynchronously. Each queue maintains a single thread and tasks are executed in the order they are posted.

    Improvements to this class might include some sort of priority scheduling for tasks. For now they execute with equal priority.

    • 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 ILogger _logger  
      private java.util.LinkedList<java.lang.Runnable> _queue  
      private boolean _shutdown  
      private static java.util.HashMap<java.lang.String,​TaskQueue> QUEUE_MAP
      A static map of the uniquely named queues.
      • Fields inherited from class java.lang.Thread

        MAX_PRIORITY, MIN_PRIORITY, NORM_PRIORITY
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private TaskQueue​(ILogger logger, java.lang.String strName)
      Use one of the getInstance() methods to create a TaskQueue.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void cancelTasks​(java.lang.Class taskClass)  
      static void emptyAndRemoveQueue​(java.lang.String strQueueName)
      Clears all the inactive tasks in the specified queue.
      void emptyQueue()
      Empty the queue
      static TaskQueue getInstance​(ILogger logger, java.lang.String strQueueName)
      Fetch a TaskQueue by name.
      static TaskQueue getInstance​(java.lang.String strQueueName)
      Fetch a TaskQueue by name.
      java.util.List<java.lang.Runnable> getTasks()
      Get a list of all the tasks in this TaskQueue.
      static boolean hasWork()  
      static void killAll()
      Stops all task queues and dumps thier queue.
      protected void log​(java.lang.Throwable t)
      Log an exception or error.
      java.lang.Runnable peekTask()
      Peek at the "current" task in the queue.
      void postTask​(java.lang.Runnable task)
      Posts a task to the queue (asynchronously).
      void postTaskAndWait​(java.lang.Runnable runnable)  
      void postTaskAndWait​(java.lang.Runnable runnable, long timeout)  
      void run()
      Do NOT ever call this! Public only by contract.
      void setILogger​(ILogger logger)  
      int size()
      Get the size of the queue at the instance this is called.
      void waitUntilAllCurrentTasksFinish()  
      • 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

      • QUEUE_MAP

        private static final java.util.HashMap<java.lang.String,​TaskQueue> QUEUE_MAP
        A static map of the uniquely named queues.
      • _queue

        private final java.util.LinkedList<java.lang.Runnable> _queue
      • _shutdown

        private boolean _shutdown
    • Constructor Detail

      • TaskQueue

        private TaskQueue​(ILogger logger,
                          java.lang.String strName)
        Use one of the getInstance() methods to create a TaskQueue.
    • Method Detail

      • getInstance

        public static TaskQueue getInstance​(java.lang.String strQueueName)
        Fetch a TaskQueue by name. If the TaskQueue doesn't already exist, creates the TaskQueue.
      • getInstance

        public static TaskQueue getInstance​(ILogger logger,
                                            java.lang.String strQueueName)
        Fetch a TaskQueue by name. If the TaskQueue doesn't already exist, creates the TaskQueue.
        Parameters:
        logger - An optional logger.
        strQueueName - The unique name for the queue.
        Returns:
        The TaskQueue associated with the specified name.
      • emptyAndRemoveQueue

        public static void emptyAndRemoveQueue​(java.lang.String strQueueName)
        Clears all the inactive tasks in the specified queue.
      • killAll

        public static void killAll()
        Stops all task queues and dumps thier queue. This is a very dangerous method and should only be called from tests.
      • postTask

        public void postTask​(java.lang.Runnable task)
        Posts a task to the queue (asynchronously).
        Parameters:
        task - A task to run in the TaskQueue's thread.
      • waitUntilAllCurrentTasksFinish

        public void waitUntilAllCurrentTasksFinish()
      • peekTask

        public java.lang.Runnable peekTask()
        Peek at the "current" task in the queue. It may or may not be running.
        Returns:
        The "current" task in the queue.
      • getTasks

        public java.util.List<java.lang.Runnable> getTasks()
        Get a list of all the tasks in this TaskQueue.
        Returns:
        A cloned list of all the tasks in this TaskQueue.
      • emptyQueue

        public void emptyQueue()
        Empty the queue
      • size

        public int size()
        Get the size of the queue at the instance this is called.
      • run

        public void run()
        Do NOT ever call this! Public only by contract.
        Specified by:
        run in interface java.lang.Runnable
        Overrides:
        run in class java.lang.Thread
      • log

        protected void log​(java.lang.Throwable t)
        Log an exception or error.
      • hasWork

        public static boolean hasWork()
      • postTaskAndWait

        public void postTaskAndWait​(java.lang.Runnable runnable,
                                    long timeout)
      • postTaskAndWait

        public void postTaskAndWait​(java.lang.Runnable runnable)
      • cancelTasks

        public void cancelTasks​(java.lang.Class taskClass)
      • setILogger

        public void setILogger​(ILogger logger)