Class CommonExecutor

java.lang.Object
java.lang.Number
java.util.concurrent.atomic.AtomicInteger
org.apache.sis.internal.system.CommonExecutor
All Implemented Interfaces:
Serializable, ThreadFactory

public final class CommonExecutor extends AtomicInteger implements ThreadFactory
The executor shared by most of Apache SIS library for relatively "heavy" operations. The operations should relatively long tasks, otherwise work-stealing algorithms may provide better performances. For example, it may be used when each computational unit is an image tile, in which case the thread scheduling overhead is small compared to the size of the computational task.

This thread pool is complementary to the ForkJoinPool used by Stream API for example. The fork-join mechanism expects computational tasks (blocking operations such as I/O are not recommended) of medium size (between 100 and 10000 basic computational steps). By contrast, the tasks submitted to this CommonExecutor may be long and involve blocking input/output operations. We use a separated thread pool for avoiding the risk that long and/or blocked tasks prevent the Java common pool from working.

This class extends AtomicInteger for opportunistic reason. Users should not rely on this implementation details.

Since:
1.1
Version:
1.1
See Also:
  • Field Details

    • PARALLELISM

      public static final int PARALLELISM
      Maximal number of threads that INSTANCE can execute. If the number of tasks is greater than this parallelism value, extraneous tasks will be queued.
    • INSTANCE

      private static final ThreadPoolExecutor INSTANCE
      The executor for background tasks. The maximum number of threads is the number of processors minus 1. The minus 1 is for increasing the chances that some CPU is still available for Java common thread pool or for JavaFX/Swing thread. In addition the caller will often do part of the work in its own thread. Threads are disposed after two minutes of inactivity.
  • Constructor Details

    • CommonExecutor

      private CommonExecutor()
      For the singleton INSTANCE.
  • Method Details

    • instance

      public static ExecutorService instance()
      Returns the executor service shared by SIS modules for most costly calculations.
      Returns:
      the executor service for SIS tasks to run in background.
    • unschedule

      public static boolean unschedule(Future<?> task)
      If the given task has been scheduled for execution but its execution did not yet started, removes it from the scheduled list. Otherwise does nothing. The given task should be one of the following values:
      Parameters:
      task - the task to remove from the list of tasks to execute.
      Returns:
      whether the given task has been removed.
    • newThread

      public Thread newThread(Runnable r)
      Invoked by INSTANCE for creating a new daemon thread. The thread will have a slightly lower priority than normal threads so that short requests for CPU power (e.g. a user action in the JavaFX/Swing thread) are processed more quickly. This is done on the assumption that tasks executed by INSTANCE are relatively long tasks, for which loosing momentously some CPU power does not make a big difference.
      Specified by:
      newThread in interface ThreadFactory
      Parameters:
      r - the runnable to assign to the thread.
      Returns:
      a thread for the executor.