Use Kotlin Sleep Function to Suspend a Thread's Execution

Use Kotlin Sleep Function to Suspend a Thread's Execution

  1. Use of the Kotlin Thread.Sleep() Function
  2. Use of the Kotlin TimeUnit Sleep() Function
  3. Use of the Kotlin Delay() Function

Java has a wait() function that pauses the current thread’s execution. The thread sleeps until another thread enters and notifies the sleeping thread.

But is there an equivalent available in Kotlin? Kotlin does not have the wait() function, but it has the sleep() function.

The Kotlin sleep() function suspends the execution of a particular coroutine. While this does not pause the execution, it allows the execution of other coroutines.

This article teaches how to use the sleep() function in Kotlin. Besides that, we will also learn another way to suspend a coroutine by using the delay() function.

Use of the Kotlin Thread.Sleep() Function

We can use the Thread.sleep() function to make a coroutine go to sleep and allow other independent coroutines to run.

The syntax for using the sleep() function is:

Thread.sleep(milliseconds)

Here’s an example where we use the Kotlin sleep() function to suspend the execution of a coroutine by 3 seconds.

fun main() {

    println("Suspending execution")

    try {
        // making the coroutine sleep for 3 seconds
        Thread.sleep(3000)
    } catch (e: InterruptedException) {
        e.printStackTrace()
    }

    println("Resuming execution")
}

Output:

Using Kotlin sleep function from Thread

Use of the Kotlin TimeUnit Sleep() Function

Like Thread.sleep(), we can also use TimeUnit to suspend the execution of a thread.

While the Thread technique only accepts the number of milliseconds as input, TimeUnit accepts 7 different time units. These 7 time units are:

  1. Nanoseconds
  2. Microseconds
  3. Milliseconds
  4. Seconds
  5. Minute
  6. Hours
  7. Days

The TimeUnit automatically converts the passed value into milliseconds, which the sleep() function accepts.

The TimeUnit methods are a part of the java.util.concurrent library. Hence, we need to import the concurrent library to use this method.

Here’s an example demonstrating the Kotlin sleep() function from TimeUnit.

import java.util.concurrent.TimeUnit

fun main() {
    println("Suspending execution")

    try {
        // // making the coroutine sleep for 3 seconds
        TimeUnit.SECONDS.sleep(3)
    } catch (e: InterruptedException) {
        e.printStackTrace()
    }
    println("Resuming execution")
}

Output:

Using Kotlin sleep function from TimeUnit

Use of the Kotlin Delay() Function

Besides the Kotlin sleep() function, we can also use the delay() function to suspend the execution of a coroutine. The delay() function also accepts milliseconds as an input.

Here’s an example to show the usage of the Kotlin delay() function.

import kotlinx.coroutines.*

fun main() = runBlocking {
    launch { // launches a new coroutine
        delay(2000L) // This delays the execution by 2 seconds allowing other coroutines to run independently
        println("welcome!") // This will be printed after the delay
    }
    print("Hi and ") // This will be printed first since the other coroutine is delayed
}

Output:

Using Kotlin delay function

Kailash Vaviya avatar Kailash Vaviya avatar

Kailash Vaviya is a freelance writer who started writing in 2019 and has never stopped since then as he fell in love with it. He has a soft corner for technology and likes to read, learn, and write about it. His content is focused on providing information to help build a brand presence and gain engagement.

LinkedIn