Class Worker

  • All Implemented Interfaces:
    Runnable

    public final class Worker
    extends Thread
    A simple worker implementation. The worker executes a assigned workload and then sleeps until another workload is set or the worker is killed.
    Author:
    Thomas Morgner
    • Constructor Detail

      • Worker

        public Worker​(int sleeptime)
        Creates a new worker.
        Parameters:
        sleeptime - the time this worker sleeps until he checks for new work.
      • Worker

        public Worker()
        Creates a new worker with an default infinite idle timeout.
    • Method Detail

      • setWorkload

        public void setWorkload​(Runnable r)
        Set the next workload for this worker.
        Parameters:
        r - the next workload for the worker.
        Throws:
        IllegalStateException - if the worker is not idle.
      • getWorkload

        public Runnable getWorkload()
        Returns the workload object.
        Returns:
        the runnable executed by this worker thread.
      • finish

        public void finish()
        Kills the worker immediately. Awakens the worker if he's sleeping, so that the worker dies without delay.
      • isAvailable

        public boolean isAvailable()
        Checks whether this worker has some work to do.
        Returns:
        true, if this worker has no more work and is currently sleeping.
      • run

        public void run()
        If a workload is set, process it. After the workload is processed, this worker starts to sleep until a new workload is set for the worker or the worker got the finish() request.
        Specified by:
        run in interface Runnable
        Overrides:
        run in class Thread
      • isFinish

        public boolean isFinish()
        Checks whether this worker has received the signal to finish and die.
        Returns:
        true, if the worker should finish the work and end the thread.