Kotlin Coroutines — Thread.sleep() vs delay

Mohak Puri
3 min readNov 19, 2018

From the past few days, I have been trying to understand Coroutines and to be honest I have struggled a lot. For those who don't know about Coroutines, in a nutshell, they are lightweight threads. I am not going to answer questions like what are Coroutines, why Coroutines etc. etc. since there are plenty of good articles related to that out there!. Rather I would be discussing something related to suspending functions.

One of the things I found difficult to wrap my head around are suspending functions. So what are they?

Kotlin has a suspend keyword which is its way of telling that this particular function is going to take some time for execution, maybe 10 seconds or even minutes who knows!. When such a function is called from a coroutine, instead of blocking until that function returns like a normal function call, it is suspended. What that means is that the Coroutine unblocks the thread that it’s running on while it waits for the result. During that time the thread is free to perform other task like executing another coroutine. As soon as the result is obtained, execution starts from where it was left.

Note : suspending functions can be called from another suspending functions or coroutines only. In case you try to call it from a normal function you will get an error.

suspending functions

Notice the arrow on line 34 it’s the IDE telling us that this is where the suspending occurs. This is the suspending point. Since delay is a suspending function which is called from another function, the enclosing function also has the suspend keyword in its declaration.

Now that we know enough, let’s see the difference between delay and Thread.sleep() using a very simple example because

Simplicity is the glory of expression — Walt Whitman

Step 1: Add these dependencies to your build.gradle file

implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.0.1'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.0.1"

Step 2: Create a new activity and add this to your layout file

Step 3: Add this to your MainActivity

Step 4: Run the app, click the button and check your Logcat.

Output: You will notice that first “launched coroutine 1” is printed then after 5 seconds “Here after a delay of 5 seconds” and then finally “launched coroutine 2” is printed.

Step 5: Update your MainActivity with the following code

Step 6: Run the app, click the button and check your Logcat again.

Output: Now you will see that first “launched coroutine 1” is printed, after that “launched coroutine 2” and when 5 seconds are over “Here after a delay of 5 seconds”.

Conclusion

Since delay is a suspending function, call to delay results in non-blocking suspension thereby allowing the other Coroutine to execute. After 5 seconds when delay’s execution is finished we continue the execution of Coroutine from the point we left.

In the case of Thread.sleep() since it is a blocking call, the Coroutine is blocked for 5 seconds and only when it is done executing the other Coroutine gets a chance to run.

Voilà! This was indeed a short one, but hopefully, it gave you a better understanding of some basic concepts of Coroutines.

Please don’t hesitate to contact me: Github and Twitter

--

--

Mohak Puri

Lead Software Engineer @INDmoney | ex GO-JEK | GSoC 2018 @openMF | Mobile | Backend | mohak1712 everywhere