Multithreading is an object utility that provides thread pools and utilities for executing tasks asynchronously in Minecraft mods.
Accessing Multithreading
Thread Pools
Multithreading provides two thread pools for different use cases:pool
General-purpose thread pool executor for running async tasks.- Core pool size: 10 threads
- Maximum pool size: 30 threads
- Keep-alive time: 0 seconds
- Queue: LinkedBlockingQueue (unbounded)
- Thread naming: “Thread 1”, “Thread 2”, etc.
scheduledPool
Scheduled executor service for delayed and repeating tasks.- Pool size: 10 threads
- Thread naming: “Thread 1”, “Thread 2”, etc.
Running Async Tasks
runAsync
Execute a task asynchronously on the thread pool.- Kotlin
- Java
runnable: Runnable- The task to execute
- Tasks execute immediately (no delay)
- Uses the general-purpose thread pool
- Non-blocking operation
submit
Submit a task and receive a Future to track completion.runnable: Runnable- The task to submit
Future<*> - A future representing the task execution
Scheduling Tasks
schedule (Single Execution)
Schedule a task to run once after a delay.r: Runnable- The task to executedelay: Long- Delay before executionunit: TimeUnit- Time unit for the delay
ScheduledFuture<*> - A future representing the scheduled task
schedule (Repeating Execution)
Schedule a task to run repeatedly at fixed intervals.r: Runnable- The task to execute repeatedlyinitialDelay: Long- Delay before first executiondelay: Long- Delay between subsequent executionsunit: TimeUnit- Time unit for delays
ScheduledFuture<*> - A future representing the scheduled task
Notes:
- Uses fixed-rate scheduling (not fixed-delay)
- Executions may overlap if task takes longer than the delay
Object Reference
Common Use Cases
Background API Requests
Delayed Actions
Periodic Tasks
Parallel Processing
Async with Callback
Timeout Handling
Debounced Actions
Thread-Safe GUI Updates
Best Practices
-
Don’t block the main thread: Use
runAsyncfor I/O operations, network requests, or heavy computations - Handle exceptions: Wrap async code in try-catch blocks to prevent silent failures
- Clean up scheduled tasks: Cancel repeating tasks when no longer needed
- Use appropriate time units: Choose the most readable unit for your delays
Notes
- Thread pools are shared across the application - avoid blocking operations
- Scheduled tasks use fixed-rate execution, not fixed-delay
- All threads are named sequentially: “Thread 1”, “Thread 2”, etc.
- The general pool can grow to 30 threads maximum
- Tasks are queued if all threads are busy
- Always handle exceptions in async code to prevent thread pool contamination