How can i wait for another thread operator complete? (coroutine+Thread)
See original GitHub issueI want to write some coroutine code which will kill callback hell,with kotlinx.coroutines.Codes like this:
import kotlinx.coroutines.experimental.*
import kotlin.concurrent.*
import kotlinx.coroutines.experimental.channels.*
fun main(args: Array<String>) = runBlocking<Unit> {
val channel = Channel<Int>()
launch(context){
thread(start = true) {
println("running from thread(): ${Thread.currentThread()}")
channel.send(1)
}
channel.receive()
}
}
But which complie error
:Suspension functions can be called only within coroutine body
I know the error message means,But i want to ask if any object that can operator in another thread an can wait result in the coroutine?So i Can write sequence code which runs under callback!
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Kotlin Coroutines: Waiting for Multiple Threads to Finish
In this tutorial, we'll see how to execute a group of tasks concurrently and wait for them to finish. We're going to use...
Read more >Kotlin Coroutines - I can not get them to work to execute on a ...
In the coroutines world, you can launch a coroutine in the same thread as the current one, it will just wait for its...
Read more >Coroutine context and dispatchers - Kotlin
Coroutines can suspend on one thread and resume on another thread. Even with a single-threaded dispatcher it might be hard to figure out...
Read more >C++20: Thread Synchronization with Coroutines
One thread prepares, in this case, a work-package another thread is waiting for. TimelineCpp20. I assume most of you use condition variables ...
Read more >Best practices for coroutines in Android
If a class is doing long-running blocking operations in a coroutine, it's in charge of moving the execution off the main thread using ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Libraries that provide a single callback are easy to wrap for use with coroutines. It is explained here: https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md#wrapping-callbacks
There is no reason to use
thread
with coroutines. Please, read the guide. It has a lot of working read-to-use examples that you can try: https://github.com/Kotlin/kotlinx.coroutines/blob/master/coroutines-guide.md