title: Kotlin coroutines - delay, how does it work?
body: <p>I am quite used to using RX to handle concurrency, but, in my current job, we have a mix of AsyncTask, Executors + Handlers, Threads and some LiveData thrown in. Now we are thinking about moving towards using Kotlin Coroutines (and in fact have started using it in certain places in the codebase).</p>
  
  <p>Therefore, I need to start wrapping my head around Coroutines, ideally drawing from my existing knowledge of concurrency tools to speed the process up.</p>
  
  <p>I have tried following the Google codelab for them and whilst it's giving me a bit of understanding it's also raising lots of unanswered questions so I've tried getting my hands dirty by writing some code, debugging and looking at log outputs.</p>
  
  <p>As I understand it, a coroutine is composed of 2 main building blocks; suspend functions which are where you do your work and coroutine contexts which is where you execute suspend functions such that you can have a handle on what dispatchers the coroutines will run on.</p>
  
  <p>Here I have some code below, that behaves as I would expect. I have set up a coroutine context using Dispatchers.Main. So, as expected, when I launch the coroutine <code>getResources</code> it ends up blocking the UI thread for 5 seconds due to the <code>Thread.sleep(5000)</code>:</p>
  
  <pre><code>private const val TAG = "Coroutines"
  
  class MainActivity : AppCompatActivity(), CoroutineScope {
      override val coroutineContext: CoroutineContext = Job() + Dispatchers.Main
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          log("onCreate", "launching coroutine")
          launch {
              val resource = getResource()
              log("onCreate", "resource fetched: $resource")
              findViewById&lt;TextView&gt;(R.id.textView).text = resource.toString()
          }
          log("onCreate", "coroutine launched")
      }
  
      private suspend fun getResource() : Int {
          log("getResource", "about to sleep for 5000ms")
          Thread.sleep(5000)
          log("getResource", "finished fetching resource")
          return 1
      }
  
      private fun log(methodName: String, toLog: String) {
          Log.d(TAG,"$methodName: $toLog: ${Thread.currentThread().name}")
      }
  }
  </code></pre>
  
  <p>When I run this code, I see the following logs:</p>
  
  <pre><code>2020-05-28 11:42:44.364 9819-9819/? D/Coroutines: onCreate: launching coroutine: main
  2020-05-28 11:42:44.376 9819-9819/? D/Coroutines: onCreate: coroutine launched: main
  2020-05-28 11:42:44.469 9819-9819/? D/Coroutines: getResource: about to sleep for 5000ms: main
  2020-05-28 11:42:49.471 9819-9819/com.example.coroutines D/Coroutines: getResource: finished fetching resource: main
  2020-05-28 11:42:49.472 9819-9819/com.example.coroutines D/Coroutines: onCreate: resource fetched: 1: main
  </code></pre>
  
  <p>As you can see, all the logs originated from the main thread, and there is a 5 second gap between the log before and after the <code>Thread.sleep(5000)</code>. During that 5 second gap, the UI thread is blocked, I can confirm this by just looking at the emulator; it doens't render any UI because <code>onCreate</code> is blocked.</p>
  
  <p>Now, if I update the <code>getResources</code> function to use the suspend fun <code>delay(5000)</code> instead of using <code>Thread.sleep(5000)</code> like so:</p>
  
  <pre><code>private suspend fun getResource() : Int {
      log("getResource", "about to sleep for 5000ms")
      delay(5000)
      log("getResource", "finished fetching resource")
      return 1
  }
  </code></pre>
  
  <p>Then what I end up seeing confuses me. I understand <code>delay</code> isn't the same as <code>Thread.sleep</code>, but because I am running it within the coroutine context which is backed by <code>Dispatchers.Main</code>, I expected to see the same result as using <code>Thread.sleep</code>.</p>
  
  <p>Instead, what I see is the UI thread is not blocked while the 5 second delay is happening, and the logs look like:</p>
  
  <pre><code>2020-05-28 11:54:19.099 10038-10038/com.example.coroutines D/Coroutines: onCreate: launching coroutine: main
  2020-05-28 11:54:19.111 10038-10038/com.example.coroutines D/Coroutines: onCreate: coroutine launched: main
  2020-05-28 11:54:19.152 10038-10038/com.example.coroutines D/Coroutines: getResource: about to sleep for 5000ms: main
  2020-05-28 11:54:24.167 10038-10038/com.example.coroutines D/Coroutines: getResource: finished fetching resource: main
  2020-05-28 11:54:24.168 10038-10038/com.example.coroutines D/Coroutines: onCreate: resource fetched: 1: main
  </code></pre>
  
  <p>I can see the UI thread is not blocked in this case as the UI renders whilst the delay is taking place and then the text view is updated after 5 seconds.</p>
  
  <p>So, my question is, how does delay, in this case, not block the UI thread (even though the logs in my suspend function still indicate that the function is running on the main thread...)</p>