TwoClocks
10/07/2018, 6:59 PMfun main(args : Array<String>) {
var taskHandle:Continuation<Unit>?=null
runBlocking { // If I run in this context, this block never exits.
launch {
while(true) {
println("about to block")
val retVal = suspendCoroutine<Unit> {
taskHandle = it;
}
println("done blocking.. on thread : ${Thread.currentThread().name}")
}
}
println("done launching")
}
println("all coroutines blocked, do work...")
repeat( 10, {
Thread.sleep(ThreadLocalRandom.current().nextInt(2000) + 500L)
taskHandle!!.resume(Unit)
})
}
Dico
10/07/2018, 10:00 PMDico
10/07/2018, 10:01 PMlaunch
without a scope is deprecated in 1.3, but this should be true in 1.3, not in versions prior to 1.3 and its development builds.krotki
10/07/2018, 10:28 PMTwoClocks
10/07/2018, 10:55 PMTwoClocks
10/07/2018, 10:56 PMkrotki
10/08/2018, 7:41 AMkrotki
10/08/2018, 7:41 AM* Runs new coroutine and **blocks** current thread _interruptibly_ until its completion.
* This function should not be used from coroutine. It is designed to bridge regular blocking code
* to libraries that are written in suspending style, to be used in `main` functions and in tests.
*
* The default [CoroutineDispatcher] for this builder in an internal implementation of event loop that processes continuations
* in this blocked thread until the completion of this coroutine.
* See [CoroutineDispatcher] for the other implementations that are provided by `kotlinx.coroutines`.
*
* When [CoroutineDispatcher] is explicitly specified in the [context], then the new coroutine runs in the context of
* the specified dispatcher while the current thread is blocked. If the specified dispatcher is an event loop of another `runBlocking`,
* the this invocation uses an outer event loop.
TwoClocks
10/08/2018, 7:09 PM