
Javaru
Kotlin Coroutine Confidence: Add `parallel/v9` (pg 125)
When I ran the parallel/v8
example on page 125, I did not get the results described of not seeing the timer’s output until the program almost exits. I got the timer output right from the start, and every second thereafter. After a few minutes of trying to figure out why, I gave up and kept reading. Once I got to page 126, I saw why. It was because the slowHash()
function was already a suspend
function with the yield()
in it. This is shown as a change to the code on page 126, but it was not a change since it was already present in the downloaded code, and therefore present for the run on page 125. Thus the different result.
Since I am running each example as soon as I first encounter it, (and up to this point it has always worked doing that ) this is unexpected behavior. As I said, I spent a few minutes trying to figure things out. I didn’t even look a the
slowHash()
function since it wasn’t a point of discussion. (And I’m not sure I would have noticed the issue had I looked at the function since that function is not discussed in v8 until it is modified on page 126.)
As a suggestion, I think the changes to the slowHash()
function on page 126 should be broken out to a parallel/v9
instance. And the parallel/v8
slowHash()
function should not be a suspend
function or have the yield()
. That way the “bad” result occurs when the code is run on page 125. Another option is to just not include the suspend
and yield()
in the parallel/v8
version and have the user make the change in their code when they read page 126. But that may be less desirable since until now, there have not been any code changes needed by the user, and it breaks the “incremental versions” paradigm in use. (And it may prevent you from testing things properly),
Assuming you go with a v9, then the sentence:
Run either of the two previous programs again with this upgraded
slowHash()
function, and everything works the way we wanted. Our extra timer coroutine can print its output every one second, and our timeout code can cancel the work promptly.
can change to something like:
You can also run the previous timeout program with this upgraded
slowHash()
function, and everything will work the way we wanted. Our timeout code can cancel the work promptly.
book-kotlin-coroutine-confidence version B3
Popular Prag Prog topics










Other popular topics









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