Kotlin Task Scheduler: Complete Overview and Usage Examples
A task scheduler is an important component of any application, allowing code to be executed on a schedule, with a delay, or periodically. In the Kotlin language, especially when developing on JVM, Android, or multiplatform projects, there are several ways to implement schedulers. In this article, we will take a detailed look at the main tools: the built-in capabilities of kotlinx-coroutines-core, java.util.Timer, java.util.concurrent.Executors, as well as the specialized library korGE for games and animations.
1. Coroutine-Based Task Scheduler (kotlinx-coroutines-core)
This is the most modern and recommended method for Kotlin applications. The kotlinx-coroutines-core library provides flexible tools for delayed and periodic code execution.
Installation
Add the dependency to build.gradle.kts:
dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.0")}Delayed Task (delay)
Simple execution delay using delay inside a coroutine:
import kotlinx.coroutines.*
fun main() = runBlocking { println("Start: ${System.currentTimeMillis()}") delay(2000) // 2 second delay println("Executed after 2 seconds: ${System.currentTimeMillis()}")}Periodic Task via launch and while
import kotlinx.coroutines.*
fun main() = runBlocking { val job = launch { repeat(5) { i -> println("Task #${i + 1}: ${System.currentTimeMillis()}") delay(1000) } } job.join()}Using Dispatchers for Background Threads
The scheduler can run on different dispatchers, for example Dispatchers.IO for network operations:
import kotlinx.coroutines.*
fun main() = runBlocking { val job = launch(Dispatchers.IO) { println("Executing on thread: ${Thread.currentThread().name}") delay(3000) println("Completed") } job.join()}2. Classic Scheduler: java.util.Timer
If you are working in a JVM environment and want to use a traditional approach, Timer and TimerTask are suitable for simple scenarios.
import java.util.*
fun main() { val timer = Timer() val task = object : TimerTask() { override fun run() { println("Periodic task: ${System.currentTimeMillis()}") } } // Start the task after 1 second, then every 2 seconds timer.schedule(task, 1000, 2000)
// Stop after 10 seconds Thread.sleep(10000) timer.cancel()}One-time Delayed Task
val timer = Timer()timer.schedule(object : TimerTask() { override fun run() { println("One-time task after 3 seconds") }}, 3000)3. Scheduler with Executors (ScheduledExecutorService)
A more powerful and flexible tool from Java Concurrency that integrates well with Kotlin.
import java.util.concurrent.*
fun main() { val scheduler = Executors.newScheduledThreadPool(2)
// Task with delay scheduler.schedule({ println("Delayed task: ${System.currentTimeMillis()}") }, 2, TimeUnit.SECONDS)
// Periodic task with fixed delay val periodicTask = scheduler.scheduleWithFixedDelay({ println("Periodic task: ${System.currentTimeMillis()}") }, 1, 3, TimeUnit.SECONDS)
// Stop after 15 seconds Thread.sleep(15000) periodicTask.cancel(false) scheduler.shutdown()}