
Javaru
Kotlin Coroutine Confidence: Add location indicators to output (pg 80)
For coffee/v3/src/main/kotlin/com/example/coffee/CoffeeApplication.kt
, on pg 80, it states that the output will be:
Done launching two child jobs!
coffeeJob.isActive is: true
coffeeJob.isActive is: true
coffeeJob.isActive is: true
Prepared some piping hot water
Prepared some freshly ground coffee
coffeeJob.isActive is: false
However, for me, the first two lines are consistently in the opposite order:
coffeeJob.isActive is: true
Done launching two child jobs!
coffeeJob.isActive is: true
coffeeJob.isActive is: true
Prepared some freshly ground coffee
Prepared some piping hot water
coffeeJob.isActive is: false
This is likely just one of those mutlithreading things. The variance might even be OS related, (Or maybe one of my CPU cores is just extra hyper today after sampling too much of the coffee we have it brewing ) But it got me wondering about if that first output was from the loop, or if it was from that last
println()
in the main()
method. (I was 99% sure it was the loop, but wanted to verify.) So I made a code change to add a location indicator:
while (coffeeJob.isActive) {
println("(loop) coffeeJob.isActive is: ${coffeeJob.isActive}")
delay(2.seconds)
}
println("(last) coffeeJob.isActive is: ${coffeeJob.isActive}")
This gave me the output of:
(loop) coffeeJob.isActive is: true
Done launching two child jobs!
(loop) coffeeJob.isActive is: true
(loop) coffeeJob.isActive is: true
Prepared some freshly ground coffee
Prepared some piping hot water
(last) coffeeJob.isActive is: false
Which cleared things up. I think this change might be a useful change in the example. I mentor some high school students in programming, and I always recommend to them when using logging or println()
statements – especially when using it to trace a program – to avoid having the exact same message in two places. That will prevent confusion, or a bad assumption, as to which line is printing that message. I think that is practical suggestion here. I think it just makes what is happening clearer to the reader.
Marked As Solved

sam-cooper
That’s a great suggestion, thanks Mark! I’ll have to remember that one for my future work too. I can definitely see why having two identical println()
calls in the same program would make it harder to match up the code against the output.
I’ll definitely make a change to this code example so that it’s clear which line of code corresponds to which line of output. I might also add a note to let readers know that their output might not match the book’s exactly.
Great feedback!
Popular Pragmatic Bookshelf topics










Other popular topics










Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /rails
- /js
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /ash
- /vscode
- /opensuse
- /centos
- /php
- /deepseek
- /html
- /scala
- /zig
- /debian
- /nixos
- /lisp
- /agda
- /sublime-text
- /react-native
- /textmate
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /spring
- /manjaro
- /django
- /diversity
- /nodejs
- /lua
- /slackware
- /julia
- /c
- /neovim