• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Head First Kotlin - Coroutines

 
Ranch Hand
Posts: 462
Scala jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I can see from the contents that you cover coroutines in appendix 1, is this just a quick overview? How do they differ from concurrency in Java, is it a totally different model?
Thanks
 
Author
Posts: 143
15
Android Python Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Will,

Yes it is just a quick introduction. Coroutines were finalised towards the end of the book, and we were determined to get some coverage.

You can think of a co-routine as a kind of lightweight thread. When you run code in a thread you are typically using a physical resource. You will normally find that threaded code will run on a distinct core in the CPU (this doesn't *have* to happen, but typically does). That means that as you use more and more threads, you steadily start to use up the resources available for running them. After a while, threads will start to block.

Co-routines provide a more advanced way of looking at multi-tasking code. Although you can run co-routines in their own threads, by using the global scope, co-routines commonly share threads. Most tasks involved doing some work and then waiting for something to happen. When this happens in a co-routines, control of the thread is rapidly switched to another co-routine, which can profitable use of the thread while the other co-routine is not using it.

This allows you to write code that looks very similar to threaded code, but which runs on the same thread. That allows you to scale the number of co-routine in a way that you never could with threads. There is no problem running hundreds of co-routines at the "same time" in a way that you never could with threads.

You can see the co-routine code for creating a drum machine here:

https://github.com/dogriffiths/HFKotlin/blob/master/appendix_a_coroutines/Drum%20Machine/src/main/kotlin/Beats.kt

The code for running "playBeats" for different instruments at the same time looks like this:

suspend fun main() {
   runBlocking {
       launch { playBeats("x-x-x-x-x-x-", "toms.aiff") }
       playBeats("x-----x-----", "crash_cymbal.aiff")
   }
}

The code for playing the Tom-toms and the cymbals will both run on the same thread, but appear to run completely in parallel, because they are using co-routines. It's this feature that makes co-routines incredibly powerful if you want to handled thousands of requests (for example) at the same time.

Thanks for the great question :-)

D+D
 
A day job? In an office? My worst nightmare! Comfort me tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic