question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can i wait for another thread operator complete? (coroutine+Thread)

See original GitHub issue

I 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:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
elizarovcommented, Jun 19, 2017

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

2reactions
elizarovcommented, Jun 16, 2017

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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found