Class ControllableWorkStealingPool

All Implemented Interfaces:
WorkerPool, AutoCloseable, Executor, ExecutorService

public class ControllableWorkStealingPool extends ThreadPoolExecutor implements WorkerPool
A controllable thread pool executor that supports actor-level job management. Unlike ForkJoinPool, this implementation uses ThreadPoolExecutor with LinkedBlockingDeque, allowing direct access to the queue for cancellation and priority execution. Features: - Track jobs per actor - Cancel all jobs for a specific actor - Submit urgent jobs to the front of the queue - Query pending job count per actor Note: This implementation does not provide work-stealing behavior like ForkJoinPool. However, for POJO-actor's use case (independent CPU-bound tasks), the performance difference is minimal, and the added control capabilities are more valuable.
Since:
1.0.0
Author:
devteam@scivics-lab.com
  • Constructor Details

    • ControllableWorkStealingPool

      public ControllableWorkStealingPool(int parallelism)
      Creates a ControllableWorkStealingPool with the specified parallelism level.
      Parameters:
      parallelism - the number of threads in the pool
  • Method Details

    • submitForActor

      public void submitForActor(String actorName, Runnable task)
      Submits a task associated with a specific actor. The task is added to the end of the queue (normal priority).
      Parameters:
      actorName - the name of the actor submitting this task
      task - the task to execute
    • submitUrgentForActor

      public void submitUrgentForActor(String actorName, Runnable task)
      Submits an urgent task to the front of the queue. The task will be executed before other pending tasks.
      Parameters:
      actorName - the name of the actor submitting this task
      task - the urgent task to execute
    • supportsCancellation

      public boolean supportsCancellation()
      Description copied from interface: WorkerPool
      Checks if this worker pool supports job cancellation per actor.
      Specified by:
      supportsCancellation in interface WorkerPool
      Returns:
      true if cancelJobsForActor() is supported
    • cancelJobsForActor

      public int cancelJobsForActor(String actorName)
      Cancels all pending jobs for a specific actor. Jobs that are already running will continue to completion. Only jobs that are still in the queue will be removed.
      Specified by:
      cancelJobsForActor in interface WorkerPool
      Parameters:
      actorName - the name of the actor whose jobs should be cancelled
      Returns:
      the number of jobs that were cancelled
    • getPendingJobCountForActor

      public int getPendingJobCountForActor(String actorName)
      Gets the number of pending jobs for a specific actor. This includes jobs in the queue but not currently executing jobs.
      Specified by:
      getPendingJobCountForActor in interface WorkerPool
      Parameters:
      actorName - the name of the actor
      Returns:
      the number of pending jobs
    • getTrackedActorCount

      public int getTrackedActorCount()
      Gets the total number of actors currently tracked.
      Returns:
      the number of actors with pending or running jobs