Skip to main content
The Multithreading utility provides easy access to thread pools and scheduled executors for running tasks asynchronously.

Accessing Multithreading

import gg.essential.api.utils.Multithreading

// Access directly as an object
Multithreading.runAsync {
    // Your async code
}

Running async tasks

runAsync

Execute a task asynchronously on the thread pool.
Multithreading.runAsync {
    // Perform expensive operation off the main thread
    val data = fetchDataFromAPI()
    processData(data)
    
    println("Task completed on thread: ${Thread.currentThread().name}")
}

submit

Submit a task and get a Future to track its completion.
import java.util.concurrent.Future

val future: Future<*> = Multithreading.submit {
    // Long-running task
    Thread.sleep(5000)
    println("Task finished")
}

// Check if completed
if (future.isDone) {
    println("Task is done")
}

// Cancel if needed
future.cancel(true)

Scheduled tasks

schedule (one-time)

Schedule a task to run once after a delay.
import java.util.concurrent.TimeUnit

// Run after 5 seconds
Multithreading.schedule(
    Runnable {
        println("This runs after 5 seconds")
    },
    5,
    TimeUnit.SECONDS
)

schedule (repeating)

Schedule a task to run repeatedly at fixed intervals.
import java.util.concurrent.TimeUnit
import java.util.concurrent.ScheduledFuture

// Run every 10 seconds, starting after 2 seconds
val scheduledTask: ScheduledFuture<*> = Multithreading.schedule(
    Runnable {
        println("This runs every 10 seconds")
    },
    2,  // Initial delay
    10, // Period
    TimeUnit.SECONDS
)

// Cancel the scheduled task when needed
scheduledTask.cancel(false)

Thread pools

pool

Access the main thread pool executor for custom operations.
import java.util.concurrent.ThreadPoolExecutor

val pool: ThreadPoolExecutor = Multithreading.pool

// Check pool status
println("Active threads: ${pool.activeCount}")
println("Queue size: ${pool.queue.size}")
println("Completed tasks: ${pool.completedTaskCount}")

scheduledPool

Access the scheduled executor service for custom scheduled operations.
import java.util.concurrent.ScheduledExecutorService
import java.util.concurrent.TimeUnit

val scheduledPool: ScheduledExecutorService = Multithreading.scheduledPool

// Use for custom scheduling
scheduledPool.scheduleWithFixedDelay(
    { println("Fixed delay task") },
    0,
    5,
    TimeUnit.SECONDS
)

Practical examples

Background data fetching

fun loadPlayerData(uuid: UUID) {
    Multithreading.runAsync {
        try {
            // Fetch data on background thread
            val data = fetchFromAPI(uuid)
            
            // Switch back to main thread to update UI
            Minecraft.getMinecraft().addScheduledTask {
                updatePlayerDisplay(data)
            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }
}

Periodic health check

import java.util.concurrent.TimeUnit

class ServerHealthMonitor {
    private var healthCheck: ScheduledFuture<*>? = null
    
    fun start() {
        healthCheck = Multithreading.schedule(
            Runnable {
                checkServerHealth()
            },
            0,  // Start immediately
            30, // Check every 30 seconds
            TimeUnit.SECONDS
        )
    }
    
    fun stop() {
        healthCheck?.cancel(false)
    }
    
    private fun checkServerHealth() {
        // Perform health check
        println("Server health: OK")
    }
}

Delayed task execution

import java.util.concurrent.TimeUnit

fun showDelayedMessage(message: String, delaySeconds: Long) {
    Multithreading.schedule(
        Runnable {
            println(message)
        },
        delaySeconds,
        TimeUnit.SECONDS
    )
}

// Usage
showDelayedMessage("This appears after 10 seconds", 10)

Thread pool configuration

The default thread pool is configured with:
  • 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.
The scheduled pool uses:
  • Pool size: 10 threads
  • Thread naming: “Thread 1”, “Thread 2”, etc.

API reference

Methods

runAsync
(runnable: Runnable) -> Unit
Execute a task asynchronously on the thread pool.
submit
(runnable: Runnable) -> Future<*>
Submit a task and return a Future to track its execution.
schedule
(r: Runnable, delay: Long, unit: TimeUnit) -> ScheduledFuture<*>
Schedule a one-time task to run after a delay.
schedule
(r: Runnable, initialDelay: Long, delay: Long, unit: TimeUnit) -> ScheduledFuture<*>
Schedule a repeating task with fixed-rate execution.
pool
ThreadPoolExecutor
The main thread pool executor for general async tasks.
scheduledPool
ScheduledExecutorService
The scheduled executor service for delayed and periodic tasks.
All async tasks run on background threads. To update UI or interact with Minecraft state, use Minecraft.getMinecraft().addScheduledTask() to switch back to the main thread.

Build docs developers (and LLMs) love