Class HeadTaskRunnable

  • All Implemented Interfaces:
    TaskRunnable, Runnable

    public class HeadTaskRunnable
    extends Object
    implements TaskRunnable
    HeadTaskRunnable is designed to be the head of the linked-list when TaskRunnable needs to be linked together. Whenever there is an insertion or deletion, HeadTaskRunnable must be locked to guarantee correct synchronization. Besides, when last element of the linked-list is removed (setNext(null)), HeadTaskRunnable should use Triggerable to remove or destroy the linked-list.
    • Field Detail

      • time

        protected Date time
      • expired

        protected volatile boolean expired
      • timeout

        protected volatile boolean timeout
      • waitCount

        protected int waitCount
      • acquireCount

        protected int acquireCount
      • owner

        protected volatile Thread owner
    • Constructor Detail

      • HeadTaskRunnable

        public HeadTaskRunnable​(Triggerable parent,
                                TaskRunnable nextTask,
                                Date time)
                         throws IllegalArgumentException
        Constructor of HeadTaskRunnable.
        Parameters:
        parent - The Triggerable interface to be run when the linked-list is empty
        nextTask - The TaskRunnable next to this TaskRunnable
        time - The time this TaskRunnable is scheduled
        Throws:
        IllegalArgumentException
    • Method Detail

      • isTimedOut

        public boolean isTimedOut()
        Returns a boolean to indicate whether the HeadTask is timeout already.
        Returns:
        a boolen to indicate whether the HeadTask is timeout.
      • isExpired

        public boolean isExpired()
        Returns a boolean to indicate whether the HeadTask is expired already.
        Returns:
        a boolean to indicate whether the HeadTask is expired.
      • getCurrentOwner

        public Thread getCurrentOwner()
        Returns the thread which currently holding this lock.
        Returns:
        the thread which currently own the lock or null.
      • acquireValidLock

        public boolean acquireValidLock()
        Tries to acquire a valid (non-expired) lock.
        Returns:
        a boolean to indicate whether it is succeed to acquire the lock.
      • setHeadTask

        public void setHeadTask​(HeadTaskRunnable headTask)
        Implements for TaskRunnable interface, no actual use for HeadTaskRunnable.
        Specified by:
        setHeadTask in interface TaskRunnable
        Parameters:
        headTask - The HeadTaskRunnable
      • getHeadTask

        public HeadTaskRunnable getHeadTask()
        Implements for TaskRunnable interface, no actual use for HeadTaskRunnable.
        Specified by:
        getHeadTask in interface TaskRunnable
        Returns:
        The head task of this linkable TaskRunnable
      • addElement

        public boolean addElement​(Object key)
        Implements for TaskRunnable interface, always return false.
        Specified by:
        addElement in interface TaskRunnable
        Parameters:
        key - Element to be added to this TaskRunnable
        Returns:
        false means nothing can be added to this TaskRunnable
      • removeElement

        public boolean removeElement​(Object key)
        Implements for TaskRunnable interface, always return false.
        Specified by:
        removeElement in interface TaskRunnable
        Parameters:
        key - Element to be removed from this TaskRunnable
        Returns:
        false means nothing can be removed from this TaskRunnable
      • isEmpty

        public boolean isEmpty()
        Implements for TaskRunnable interface, always return false.
        Specified by:
        isEmpty in interface TaskRunnable
        Returns:
        true means this TaskRunnable is always empty
      • setNext

        public void setNext​(TaskRunnable task)
        Sets the TaskRunnable next to this TaskRunnable in the linked-list.
        Specified by:
        setNext in interface TaskRunnable
        Parameters:
        task - The next TaskRunnable
      • setPrevious

        public void setPrevious​(TaskRunnable task)
        Implements for TaskRunnable interface, There is no previous element for HeadTaskRunnable.
        Specified by:
        setPrevious in interface TaskRunnable
        Parameters:
        task - The previous TaskRunnable
      • next

        public TaskRunnable next()
        Returns the TaskRunnable next to this TaskRunnable in the linked-list.
        Specified by:
        next in interface TaskRunnable
        Returns:
        next TaskRunnable object or null if it is not set
      • previous

        public TaskRunnable previous()
        Implements for TaskRunnable interface, there is no previous element for HeadTaskRunnable.
        Specified by:
        previous in interface TaskRunnable
        Returns:
        null means there is no previous element
      • getRunPeriod

        public long getRunPeriod()
        Implements for TaskRunnable interface, HeadTaskRunnable doesn't have a run period.
        Specified by:
        getRunPeriod in interface TaskRunnable
        Returns:
        -1 means the task only will be run once
      • cancel

        public void cancel()
        Implements for TaskRunnable interface.
        Specified by:
        cancel in interface TaskRunnable
      • setTrigger

        public void setTrigger​(Triggerable parent)
        Sets the Triggerable interface which will be run when the linked-list is empty.
        Parameters:
        parent - The Triggerable interface to be run when the linked-list is empty
      • setTail

        public void setTail​(TaskRunnable task)
        Sets the task which is the tail of the list. It is for internal use only.
        Parameters:
        task - The task which is at the tail of the list.
      • tail

        public TaskRunnable tail()
        Returns the Task which is at the tail of the list.
        Returns:
        The task which is at the tail of the list.
      • scheduledExecutionTime

        public long scheduledExecutionTime()
        Returns the time which this HeadTaskRunnable is scheduled.
        Specified by:
        scheduledExecutionTime in interface TaskRunnable
        Returns:
        The long value which represents the time this task is scheduled
      • run

        public void run()
        Implements for TaskRunnable interface, just run the next TaskRunnable.
        Specified by:
        run in interface Runnable