dhmitchell

dhmitchell

Kotlin Coroutine Confidence: TimerApplication: won't app finish before timer?

In the example “timers/v7/src/main/kotlin/com/example/timers/TimerApplication.kt”
Why doesn’t the program just exit after queuing the timer.schedule rather than waiting for them to callback?

Marked As Solved

sam-cooper

sam-cooper

Author of Kotlin Coroutine Confidence

Great question! As I very briefly mention a couple of pages earlier, the java.util.Timer() class has its own thread, which it uses to run all its callbacks. It’s the existence of this thread which keeps the program running. When we reach the callback, we call timer.cancel(), which shuts down the timer’s thread and allows the program to exit.

I’ll see if I can update the text to make it a little clearer why the program behaves this way.

The fact that active threads keep the program alive is pretty useful for our Timer code examples, but it doesn’t have to apply to all threads. You can also configure a thread as a daemon to allow the program to exit without waiting for the thread to finish. You’ll spot that we have to do this for the Retrofit HTTP client we use later on in the same chapter. Setting up all the code examples so that they exit correctly when all our coroutines and callbacks have finished—but not before—was actually a really interesting challenge!

Popular Pragmatic Bookshelf topics Top

abtin
page 20: … protoc command… I had to additionally run the following go get commands in order to be able to compile protobuf code using go...
New
Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
simonpeter
When I try the command to create a pair of migration files I get an error. user=> (create-migration "guestbook") Execution error (Ill...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
raul
Hi Travis! Thank you for the cool book! :slight_smile: I made a list of issues and thought I could post them chapter by chapter. I’m rev...
New
New
taguniversalmachine
Hi, I am getting an error I cannot figure out on my test. I have what I think is the exact code from the book, other than I changed “us...
New
dtonhofer
@parrt In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4....
New
davetron5000
Hello faithful readers! If you have tried to follow along in the book, you are asked to start up the dev environment via dx/build and ar...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
Rainer
My first contact with Erlang was about 2 years ago when I used RabbitMQ, which is written in Erlang, for my job. This made me curious and...
New
PragmaticBookshelf
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New