Class ManagedThreadPool

All Implemented Interfaces:
WorkerPool, AutoCloseable, Executor, ExecutorService

public class ManagedThreadPool extends ThreadPoolExecutor implements WorkerPool
A managed thread pool for CPU-intensive tasks in POJO-actor.

Virtual threads in POJO-actor excel at lightweight operations (message passing, state updates), but CPU-intensive computations should be delegated to a real thread pool to avoid blocking the virtual thread scheduler.

This class provides a managed pool of real OS threads with actor-level job management capabilities:

  • Track jobs per actor
  • Cancel all pending jobs for a specific actor
  • Submit urgent jobs to the front of the queue
  • Query pending job count per actor

Usage example:


 ActorSystem system = new ActorSystem("system", 4); // 4 CPU threads

 // Light operation → virtual thread (default)
 actor.tell(a -> a.updateCounter());

 // Heavy computation → managed thread pool
 CompletableFuture<Double> result = actor.ask(
     a -> a.performMatrixMultiplication(),
     system.getManagedThreadPool()
 );
 
Since:
2.12.0
Author:
devteam@scivicslab.com
  • Constructor Details

    • ManagedThreadPool

      public ManagedThreadPool(int parallelism)
      Creates a ManagedThreadPool with the specified parallelism level.
      Parameters:
      parallelism - the number of threads in the pool (typically matches CPU core count)
  • 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