Package com.tnsai.thread
Class ThreadManager
java.lang.Object
com.tnsai.thread.ThreadManager
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classstatic final record -
Method Summary
Modifier and TypeMethodDescriptionvoidaddGlobalListener(Consumer<ThreadEvent> listener) Add a global event listener that receives events from all threads.static ThreadManager.Builderbuilder()voidcleanup()Clean up completed threads older than retention period.voidclose()static ThreadManagercreate()Create a simple manager with defaults.static ThreadManagercreate(com.tnsai.state.Checkpointer checkpointer) Create a manager with a checkpointer.createThread(ThreadSpec config) Create a new thread with the given configuration.createThread(String threadId, ThreadSpec config) Create a thread with a specific ID.voidForce cleanup of all terminal threads.Get active (non-terminal) threads.Get all threads.Get recoverable thread IDs (those with checkpoints but not active).getStats()Get thread statistics.Get a thread by ID.getThreadOrThrow(String threadId) Get a thread by ID, throwing if not found.getThreadsByState(ThreadState state) Get threads by state.booleanCheck if a thread exists.booleanrecoverThread(String threadId) Recover a thread from its checkpoint.recoverThread(String threadId, ThreadSpec config) Recover a thread with a specific configuration.voidremoveGlobalListener(Consumer<ThreadEvent> listener) Remove a global event listener.voidshutdown()Shutdown the manager.
-
Method Details
-
builder
-
create
Create a simple manager with defaults. -
create
Create a manager with a checkpointer. -
createThread
Create a new thread with the given configuration.- Throws:
ThreadException- if max concurrent threads exceeded
-
createThread
Create a thread with a specific ID. -
getThread
Get a thread by ID. -
getThreadOrThrow
Get a thread by ID, throwing if not found. -
getAllThreads
Get all threads. -
getActiveThreads
Get active (non-terminal) threads. -
getThreadsByState
Get threads by state. -
hasThread
Check if a thread exists. -
recoverThread
Recover a thread from its checkpoint.- Parameters:
threadId- The thread ID to recover- Returns:
- The recovered thread
- Throws:
ThreadException- if no checkpoint found
-
recoverThread
Recover a thread with a specific configuration. -
getRecoverableThreadIds
Get recoverable thread IDs (those with checkpoints but not active). -
addGlobalListener
Add a global event listener that receives events from all threads. -
removeGlobalListener
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
Get thread statistics. -
shutdown
public void shutdown()Shutdown the manager. -
close
public void close()- Specified by:
closein interfaceAutoCloseable
-
isShutdown
public boolean isShutdown()
-