Package tenapull.run

Class JobFactory

java.lang.Object
tenapull.run.JobFactory

public class JobFactory
extends java.lang.Object
Manages all jobs and job scheduling for the main thread. Since worker threads require a non-null JobFactory instance, and JobFactory instances can only be constructed privately by JobFactory static methods, it is entirely upto the JobFactory static methods (run by the main thread) to create and manage the worker threads and their jobs.
  • Field Summary

    Fields 
    Modifier and Type Field Description
    static long MAX_MAIN_WAIT_TIME
    The maximum amount of time the main thread will wait before iterating over its tasks again.
    static long MAX_NEW_JOB_PROCESSING_TIME_MS
    Timeout to stop processing new jobs.
    static int NUM_WORKER_THREADS
    The number of worker threads to keep active at once
  • Method Summary

    Modifier and Type Method Description
    Job getNextJob()
    For worker threads to obtain their job.
    static void init​(Var<tenapull.run.JobFactory.Init> holder)
    May only be invoked by the main thread once, to obtain an Init instance that will placed inside the provided container.
    static void notifyOfJobExit​(Job source)
    Called by all jobs when they exit, either due to returning false from isReady, an exception being thrown, or being completed.
    static void notifyOfJobProvider​(Job source)
    Notifies the main thread that a source job is providing new jobs.

    Methods inherited from class java.lang.Object

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

    • MAX_NEW_JOB_PROCESSING_TIME_MS

      public static final long MAX_NEW_JOB_PROCESSING_TIME_MS
      Timeout to stop processing new jobs. This forces the main thread to iterate over other tasks before returning to process the remaining new jobs, in case e.g. there are any delayed jobs that need to be queued as a readyJob.
      See Also:
      Constant Field Values
    • NUM_WORKER_THREADS

      public static final int NUM_WORKER_THREADS
      The number of worker threads to keep active at once
      See Also:
      Constant Field Values
    • MAX_MAIN_WAIT_TIME

      public static final long MAX_MAIN_WAIT_TIME
      The maximum amount of time the main thread will wait before iterating over its tasks again. Probably not necessary, but included as a fail-safe in case of a bug in the synchronization/wait/notify logic.
      See Also:
      Constant Field Values
  • Method Details

    • getNextJob

      public Job getNextJob()
      For worker threads to obtain their job. If there are no jobs currently available, the worker thread may be repurposed to run finalizer tasks for InstancesTracker
      Returns:
      the next job
    • init

      public static void init​(Var<tenapull.run.JobFactory.Init> holder)
      May only be invoked by the main thread once, to obtain an Init instance that will placed inside the provided container. The Init instance will be used by Main as the entry point into the JobsFactory jobs management loop.
      Parameters:
      holder - the holder
    • notifyOfJobExit

      public static void notifyOfJobExit​(Job source)
      Called by all jobs when they exit, either due to returning false from isReady, an exception being thrown, or being completed. Notifies the main thread that there may be new tasks to check on.
      Parameters:
      source - the source
    • notifyOfJobProvider

      public static void notifyOfJobProvider​(Job source)
      Notifies the main thread that a source job is providing new jobs. The source job will be placed in the queue of new job providers, so the main thread can go fetch these jobs when it is able to.
      Parameters:
      source - the source job, providing new jobs