• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Head First Kotlin - Coroutines  RSS feed

 
Ranch Hand
Posts: 447
Java jQuery Scala
  • Mark post as helpful
  • send pies
  • 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: 105
5
Android Python Ruby
  • Mark post as helpful
  • send pies
  • 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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!