Class AbstractSubsystem

  • All Implemented Interfaces:
    Subsystem
    Direct Known Subclasses:
    AbstractIndependentSubsystem, AbstractPooledSubsystem

    public abstract class AbstractSubsystem
    extends java.lang.Object
    implements Subsystem
    An abstract implementation of the Subsystem interface.

    Manages acquiring and releasing required resources and handles starting/stopping tasks. Subclasses are expected to define the semantics of how tasks are run (shared or independent workers, one-shot or repeating, etc).

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      protected class  AbstractSubsystem.RunTask
      The task for this Subsystem to run.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected java.util.concurrent.Future<?> job
      The Future representing this Subsystem's task.
      protected java.util.Set<java.util.concurrent.locks.Lock> resourceLocks
      A list of resourceLocks which this Subsystem requires.
      protected static int THREAD_PRIORITY  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      protected void acquireResources()
      Helper method to acquire all defined resources.
      void cancel()
      Stop this Subsystem's background task.
      protected abstract java.util.concurrent.ScheduledExecutorService getExecutorService()
      Get the executor service which executes this subsystem's tasks.
      java.util.concurrent.Future<?> getJob()
      Get the job representing the execution of this Subsystem's task.
      boolean isCancelled()
      Check if this subsystem's task has been cancelled.
      boolean isScheduled()
      Check if this subsystem's task is scheduled to execute.
      boolean isStarted()
      Check if this subsystem's task has been started.
      protected void releaseResources()
      Helper method to release all defined resources.
      void require​(java.lang.Object resource)
      Define a resource which this Subsystem requires exclusive access to while its task runs.
      • Methods inherited from class java.lang.Object

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

      • resourceLocks

        protected final java.util.Set<java.util.concurrent.locks.Lock> resourceLocks
        A list of resourceLocks which this Subsystem requires.
        See Also:
        ResourceTracker
      • job

        protected java.util.concurrent.Future<?> job
        The Future representing this Subsystem's task. 'null' if the subsystem has never yet been started. This can be used to check if the subsystem has ever been started, or to cancel scheduled jobs.
        See Also:
        Subsystem.start()
    • Constructor Detail

      • AbstractSubsystem

        public AbstractSubsystem()
    • Method Detail

      • require

        public final void require​(java.lang.Object resource)
        Define a resource which this Subsystem requires exclusive access to while its task runs.
        Specified by:
        require in interface Subsystem
        Parameters:
        resource - the required resource.
      • acquireResources

        protected final void acquireResources()
                                       throws java.lang.InterruptedException
        Helper method to acquire all defined resources.
        Throws:
        java.lang.InterruptedException - if interrupted while holding any resource lock
      • releaseResources

        protected final void releaseResources()
        Helper method to release all defined resources.
      • getJob

        public final java.util.concurrent.Future<?> getJob()
        Get the job representing the execution of this Subsystem's task.
        Specified by:
        getJob in interface Subsystem
        Returns:
        the Future job.
      • isCancelled

        public final boolean isCancelled()
        Check if this subsystem's task has been cancelled.
        Specified by:
        isCancelled in interface Subsystem
        Returns:
        true iff cancelled.
      • isStarted

        public final boolean isStarted()
        Check if this subsystem's task has been started.
        Specified by:
        isStarted in interface Subsystem
        Returns:
        true iff started.
      • isScheduled

        public final boolean isScheduled()
        Description copied from interface: Subsystem
        Check if this subsystem's task is scheduled to execute.
        Specified by:
        isScheduled in interface Subsystem
        Returns:
        true iff scheduled.
      • cancel

        public final void cancel()
        Stop this Subsystem's background task. If this is a one-shot subsystem, the task will never be run if it has not already started. If it has already started, the task may be cancelled partway through execution. If it has already completed, this has no effect. For repeating subsystems, tasks in progress may be cancelled partway through execution, and in any case, future tasks will no longer be run.
        Specified by:
        cancel in interface Subsystem
      • getExecutorService

        protected abstract java.util.concurrent.ScheduledExecutorService getExecutorService()
        Get the executor service which executes this subsystem's tasks. This service may or may not be shared with other subsystems; for Independent subsystems it is never shared, and for Pooled subsystems it is always shared.
        Returns:
        the executor service.
        See Also:
        AbstractIndependentSubsystem, AbstractPooledSubsystem