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.