Class ImagingTaskProcessor

  • Direct Known Subclasses:
    LocalImagingTaskProcessor

    public abstract class ImagingTaskProcessor
    extends java.lang.Object
    Author:
    Fabrizio Giudici
    • Field Detail

      • maxWorkers

        protected static int maxWorkers
        The max number of workers.
      • freeWorkers

        protected int freeWorkers
        The current number of free workers.
      • lock

        protected final java.lang.Object lock
        A lock used to manipulate the inner state.
    • Constructor Detail

      • ImagingTaskProcessor

        protected ImagingTaskProcessor()
    • Method Detail

      • getInstance

        public static ImagingTaskProcessor getInstance()
        Gets the singleton instance of the processor.
        Returns:
        the ImagingTaskProcessor
      • setDefault

        public static void setDefault​(java.lang.Class<? extends ImagingTaskProcessor> defaultClass)
        Sets the default implementation of the ImagingTaskProcessor
        Parameters:
        defaultClass - the implementation class
      • setMaxWorkers

        public static void setMaxWorkers​(int maxWorkers)
        Sets a limit to the number of concurrent workers.
        Parameters:
        maxWorkers - the max. number of workers
      • getMaxWorkers

        public int getMaxWorkers()
      • getWorkerCount

        public abstract int getWorkerCount()
      • getWorkerIds

        public abstract java.util.Collection<java.io.Serializable> getWorkerIds()
      • isDistributed

        public abstract boolean isDistributed()
        Returns true if the tasks will be executed in a distributed context (i.e. with different physical nodes - in single-node, multi-core contexts this method returns false.
      • hasFileAccess

        public abstract boolean hasFileAccess()
        Returns true if the tasks will be executed in a distributed context with the support of a distributed file system - that is, if each task is guaranteed to have a Path access to the same set of data. Or if the context is a single-node, multi-core system, which of course has a single filesystem.
      • processingResourcesAvailable

        public boolean processingResourcesAvailable()
      • post

        public void post​(java.util.Collection<? extends ImagingTask> tasks)
        Posts a new task into the queue. The task will be scheduled later as soon there are favorable conditions. This method returns immediately.
        Parameters:
        task - the ImagingTask to post
      • post

        public void post​(ImagingTask task)
        Posts a new task into the queue. The task will be scheduled later as soon there are favorable conditions. This method returns immediately.
        Parameters:
        task - the ImagingTask to post
      • postWithPriority

        public void postWithPriority​(java.util.Collection<? extends ImagingTask> tasks)
        Parameters:
        task - the ImagingTask to post
      • postWithPriority

        public void postWithPriority​(ImagingTask task)
        Parameters:
        task - the ImagingTasks to post
      • cancellPendingTasks

        public final <T extends ImagingTask> java.util.Collection<T> cancellPendingTasks​(java.lang.Class<T> taskClass)
        Cancels all the pending tasks of the given type. This method returns immediately. Tasks in running state will be completed.
        Parameters:
        taskClass - the kind of task to remove (null means all)
        Returns:
        the list of completed tasks
      • getPendingTaskCount

        public int getPendingTaskCount​(java.lang.Class<? extends ImagingTask> taskClass)
        Returns the number of pending tasks of the given class.
        Parameters:
        taskClass - the class of the task (null for any class)
        Returns:
        the task count
      • getRunningTaskCount

        public int getRunningTaskCount​(java.lang.Class<? extends ImagingTask> taskClass)
        Returns the number of running tasks of the given class.
        Parameters:
        taskClass - the class of the task (null for any class)
        Returns:
        the task count
      • getCompletedTaskCount

        public int getCompletedTaskCount​(java.lang.Class<? extends ImagingTask> taskClass)
        Returns the number of completed tasks of the given class. This number is decreased as popCompletedTask() is called.
        Parameters:
        taskClass - the class of the task (null for any class)
        Returns:
        the task count
      • popCompletedTask

        public <T extends ImagingTask> T popCompletedTask​(java.lang.Class<T> taskClass)
        Pops a completed task out of the list. This will decrease the count of completed task. This operation is usually performed by the controller to consume the task result and eventually schedule a new task.
        Parameters:
        taskClass - the class of the task
        Returns:
        the completed task
        Throws:
        java.util.NoSuchElementException - if no tasks of the given class is available
      • getStatistics

        public Statistics getStatistics()
      • changeFreeWorkerCount

        protected void changeFreeWorkerCount​(int delta)
      • getNextTask

        protected final ImagingTask getNextTask​(java.io.Serializable workerId,
                                                boolean remoteExecution)
        Returns the next pending task to execute. This method blocks until a pending task is available.
        Returns:
        the task to execute
      • notifyTaskCompleted

        protected final void notifyTaskCompleted​(ImagingTask task)

        Notifies that a task has been completed. The task is removed from the list of running tasks and added into the list of completed tasks. If an ImagingTaskProcessorController has been specified, it is notified too.

        This method is able to filter out duplicated tasks (that could occur with some distributed implementations.

        It is important that this method blocks until the controller has been invoked - otherwise e.g. the Computer Server implementation could fail, for instance if there are no running tasks and the controller has not been able to post new tasks yet. In this circumstances, the facility erroneously thinks that everything is over and quits.

        Parameters:
        task - the completed task