Class AbstractWorker<T extends Job>

  • Direct Known Subclasses:
    PriorityWorker, Worker

    public abstract class AbstractWorker<T extends Job>
    extends java.lang.Object
    Class that uses a single thread to execute scheduled jobs.

    Subclasses provide the BlockingQueue implementation, which allows for controlling how jobs get scheduled (e.g., FIFO or priority-based).

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected AbstractWorker​(java.util.concurrent.BlockingQueue<T> queue, boolean isPersistentThread, java.lang.String name, boolean shareThreadPool, TaskMonitor monitor)
      Constructs a new Worker with the given name.
    • Constructor Detail

      • AbstractWorker

        protected AbstractWorker​(java.util.concurrent.BlockingQueue<T> queue,
                                 boolean isPersistentThread,
                                 java.lang.String name,
                                 boolean shareThreadPool,
                                 TaskMonitor monitor)
        Constructs a new Worker with the given name.
        Parameters:
        queue - the queue into which jobs will be place. e.g. PriorityBlockingQueue or LinkedBlockingQueue
        isPersistentThread - if true, the worker thread will stay around when idle; false means that the thread will go away if not needed. Should be true for high frequency usage.
        name - the name of this worker. The thread that executes the jobs will have this name.
        shareThreadPool - true signals to use the given name to find/create a thread pool that can be shared throughout the system.
        monitor - the task monitor that allows for cancelling of jobs.
    • Method Detail

      • setTaskMonitor

        public void setTaskMonitor​(TaskMonitor monitor)
      • schedule

        public void schedule​(T job)
        Schedules the job for execution. Jobs will be processed in priority order. The highest priority jobs are those with the lowest value return by the job's getPriority() method. (i.e. the job with priority 0 will be processed before the job with priority 1)
        Parameters:
        job - the job to be executed.
      • clearAllJobs

        public void clearAllJobs()
        Clears any pending jobs and cancels any currently executing job.
      • clearAllJobsWithInterrupt_IKnowTheRisks

        public void clearAllJobsWithInterrupt_IKnowTheRisks()
        Clears any pending jobs and cancels any currently executing job.

        Warning: Calling this method may leave the program in a bad state. Thus, it is recommended that you only do so when you known that any job that could possibly be scheduled does not manipulate sensitive parts of the program; for example, opening file handles that should be closed before finishing.

        If you are unsure about whether your jobs handle interrupt correctly, then don't use this method.

      • clearPendingJobs

        public void clearPendingJobs()
        Clears any jobs from the queue that have not yet been run. This does not cancel the currently running job.
      • dispose

        public void dispose()
        Disposes this worker and terminates its thread.
      • isDisposed

        public boolean isDisposed()
      • setBusyListener

        public void setBusyListener​(BusyListener listener)
      • isBusy

        public boolean isBusy()
      • waitUntilNoJobsScheduled

        public void waitUntilNoJobsScheduled​(int maxWait)
        This method will block until there are no scheduled jobs in this worker. This method assumes that all jobs have a priority less than Long.MAX_VALUE.

        For a non-priority queue, this call will not wait for jobs that are scheduled after this call was made.