Class ThreadManager

java.lang.Object
com.tnsai.thread.ThreadManager
All Implemented Interfaces:
AutoCloseable

public class ThreadManager extends Object implements AutoCloseable
Manages the lifecycle of AgentThreads.

Features:

  • Thread creation and registration
  • Thread lookup by ID
  • Global resource limits (max concurrent threads)
  • Background cleanup of completed threads
  • Thread recovery from checkpoints
  • Global event listeners

Usage Example


 // Create manager with checkpointer
 ThreadManager manager = ThreadManager.builder()
     .checkpointer(new InMemoryCheckpointer())
     .maxConcurrentThreads(100)
     .cleanupInterval(Duration.ofMinutes(5))
     .build();

 // Create threads
 AgentThread thread1 = manager.createThread(ThreadSpec.defaults());
 AgentThread thread2 = manager.createThread(ThreadSpec.shortLived());

 // Find thread by ID
 Optional<AgentThread> found = manager.getThread(thread1.getThreadId());

 // Get all active threads
 List<AgentThread> active = manager.getActiveThreads();

 // Recover a thread from checkpoint
 AgentThread recovered = manager.recoverThread("thread-abc123");

 // Shutdown
 manager.shutdown();
 
Since:
0.0.2
  • Method Details

    • builder

      public static ThreadManager.Builder builder()
    • create

      public static ThreadManager create()
      Create a simple manager with defaults.
    • create

      public static ThreadManager create(com.tnsai.state.Checkpointer checkpointer)
      Create a manager with a checkpointer.
    • createThread

      public AgentThread createThread(ThreadSpec config)
      Create a new thread with the given configuration.
      Throws:
      ThreadException - if max concurrent threads exceeded
    • createThread

      public AgentThread createThread(String threadId, ThreadSpec config)
      Create a thread with a specific ID.
    • getThread

      public Optional<AgentThread> getThread(String threadId)
      Get a thread by ID.
    • getThreadOrThrow

      public AgentThread getThreadOrThrow(String threadId)
      Get a thread by ID, throwing if not found.
    • getAllThreads

      public List<AgentThread> getAllThreads()
      Get all threads.
    • getActiveThreads

      public List<AgentThread> getActiveThreads()
      Get active (non-terminal) threads.
    • getThreadsByState

      public List<AgentThread> getThreadsByState(ThreadState state)
      Get threads by state.
    • hasThread

      public boolean hasThread(String threadId)
      Check if a thread exists.
    • recoverThread

      public AgentThread recoverThread(String threadId)
      Recover a thread from its checkpoint.
      Parameters:
      threadId - The thread ID to recover
      Returns:
      The recovered thread
      Throws:
      ThreadException - if no checkpoint found
    • recoverThread

      public AgentThread recoverThread(String threadId, ThreadSpec config)
      Recover a thread with a specific configuration.
    • getRecoverableThreadIds

      public List<String> getRecoverableThreadIds()
      Get recoverable thread IDs (those with checkpoints but not active).
    • addGlobalListener

      public void addGlobalListener(Consumer<ThreadEvent> listener)
      Add a global event listener that receives events from all threads.
    • removeGlobalListener

      public void removeGlobalListener(Consumer<ThreadEvent> listener)
      Remove a global event listener.
    • cleanup

      public void cleanup()
      Clean up completed threads older than retention period.
    • forceCleanup

      public void forceCleanup()
      Force cleanup of all terminal threads.
    • getStats

      public ThreadManager.ThreadStats getStats()
      Get thread statistics.
    • shutdown

      public void shutdown()
      Shutdown the manager.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
    • isShutdown

      public boolean isShutdown()