Execute a function after initial delay or periodically in Kotlin
This article explores different ways to execute a function after an initial delay or periodically in Kotlin.
1. Using Timer class
The Timer class offers the facility for threads to schedule tasks for future execution in a background thread. For example, the println task is scheduled for one-time execution after a delay of 1 second below.
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.* fun main() { println("Executing block after 1 second..") Timer().schedule(object : TimerTask() { override fun run() { println("1 second elapsed") } }, 1000) } |
The code can be further shortened as follows:
|
1 2 3 4 5 6 7 8 9 10 |
import java.util.* import kotlin.concurrent.schedule fun main() { println("Executing block after 1 second..") Timer().schedule(1000) { println("1 second elapsed") } } |
Note that tasks may be scheduled for repeated execution at regular intervals. For example, the following code invokes the foo() function every 1 second.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.* fun foo() { println("Running") } fun main() { Timer().scheduleAtFixedRate( object : TimerTask() { override fun run() { foo() } }, 0, 1000) } |
Output:
Running
Running
…
…
This can be further shortened as below:
|
1 2 3 4 5 6 7 8 9 10 11 12 |
import java.util.* import kotlin.concurrent.scheduleAtFixedRate fun foo() { println("Running") } fun main() { Timer().scheduleAtFixedRate(0, 1000) { foo() } } |
Output:
Running
Running
…
…
2. Using Executors class
Alternatively, we can get an executor that can schedule commands to run after a given initial delay or to execute periodically. For example, the following code calls the foo() function after a delay of 1 second using the schedule() function.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.concurrent.Executors import java.util.concurrent.TimeUnit fun foo() { println("Running") } fun main() { println("Start…") val executorService = Executors.newSingleThreadScheduledExecutor() executorService.schedule({ foo() }, 1, TimeUnit.SECONDS) } |
To schedule a task to run periodically, use the scheduleAtFixedRate() function. For example, the following code invokes the foo() function every 1 second.
|
1 2 3 4 5 6 7 8 9 10 11 |
import java.util.concurrent.Executors import java.util.concurrent.TimeUnit fun foo() { println("Running") } fun main() { val executorService = Executors.newSingleThreadScheduledExecutor() executorService.scheduleAtFixedRate({ foo() }, 0, 1, TimeUnit.SECONDS) } |
Output:
Running
Running
…
…
3. Using Thread.sleep() function
Finally, we can use the Thread.sleep() function to temporarily cease execution of the current thread for the specified number of milliseconds.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
fun wait(ms: Int) { try { Thread.sleep(ms.toLong()) } catch (ex: InterruptedException) { Thread.currentThread().interrupt() } } fun main() { println("Executing block after 1 second..") wait(1000) println("1 second elapsed…") } |
Output:
Start…
1 second elapsed…
That’s all about executing a function after an initial delay or periodically in Kotlin.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)