So the `Thread.sleep` isn't blocking its suspendin...
# vertx
a
So the
Thread.sleep
isn't blocking its suspending the coroutine?
d
Run an asynchronous [block] on a worker threads and awaits the result. * * The [block] is executed and should return an object or throw an exception. * * - when an object is returned, it is returned from the
awaitBlocking
call * - when an exception is thrown, it is thrown from the
awaitBlocking
call * *
Copy code
* val s = awaitBlocking {
 *   Thread.sleep(1000)
 *   "some-string"
 * }
 *
* * The coroutine will be suspend until the block is executed, this action do not block vertx's eventLoop. * From the function docs... it blocks its thread in a worker thread, it just doesn't block the event loop. Of course, it's always better to use delay for that, only non-async long running computations should be run on workers
What 'will be suspend' means is on the event loop, but the actual operation is blocking the worker with the sleep
a
so the 'move' from event loop to worker thread is done under the hood?
d
Here's the implementation
Copy code
suspend fun <T> awaitBlocking(block: () -> T) : T {
  return awaitResult<T> { handler ->
    val ctx = Vertx.currentContext()
    ctx.executeBlocking<T>({ fut ->
      fut.complete(block())
    }, { ar ->
      handler.handle(ar)
    })
  }
}