Kotlin's Job: A Handle to a Background Task

A Job is a handle to a coroutine, letting you manage its lifecycle. You get one from launch to control background tasks like network calls. The key footgun: a child's *failure* (Exception) cancels its parent, but a child's *cancellation* does not.
Why it exists
Coroutines run concurrently. We need a way to track, manage, and cancel these background operations without blocking threads or leaking resources. A Job provides this structured control, creating a clear lifecycle for asynchronous work and preventing orphaned tasks.
The mental model
Think of a Job as the supervisor of a background task. It doesn't hold a result (that's a Deferred), but it holds the task's status and gives you the power to tell it to stop. Jobs can also supervise other Jobs, creating a parent-child hierarchy like a company's org chart.
How it works
A Job has a lifecycle with states like Active, Cancelling, and Completed. It's typically created in an Active state by the launch coroutine builder. Jobs form parent-child hierarchies that dictate cancellation behavior. First, if a parent Job is cancelled, it immediately cancels all its children. Second, if a child Job fails with an exception (anything other than CancellationException), it cancels its parent, which in turn cancels all other sibling Jobs. Third, if a child is cancelled normally via cancel() (which uses CancellationException), it does not affect the parent. This allows a parent to manage its children without being taken down.
When to use it
Use a Job whenever you launch a coroutine for its side effects and need a handle to manage it. This is common for fire-and-forget operations, UI updates, or any background work where you don't need a direct return value. The launch coroutine builder is your primary source of Jobs.
When not to use it
Don't use a Job when you need to get a result back from the coroutine. For that, use Deferred, which is a type of Job that promises a value upon completion. You get a Deferred from the async coroutine builder.
One canonical example
A parent coroutine launches two child Jobs for a single operation. If child one makes a network call and fails with an IOException, it not only cancels itself but also propagates the failure up to the parent. The parent then cancels, which in turn immediately cancels the second child Job, even if it was running correctly. This ensures the entire unit of work fails together. If you instead called childTwo.cancel(), only child two would stop; the parent and child one would continue running.
Interview question
What is the key distinction in how a child coroutine's termination impacts its parent Job?
- a.An unhandled exception from a child cancels the parent, while a child's normal cancellation does not.Correct
- b.A child's normal cancellation (via cancel()) cancels the parent, but an unhandled exception does not.
- c.Neither a child's normal cancellation nor an unhandled exception will ever cancel the parent.
- d.Both a child's normal cancellation and an unhandled exception will always cancel the parent.
Why? this is the answer
The card states that if a child Job fails with an exception (other than CancellationException), it cancels its parent. However, if a child is cancelled normally via cancel() (which uses CancellationException), it does not affect the parent.
Just read this? Test yourself on what you have been reading.
Read the original → kotlinlang.org
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on kotlin — each one lists the topics its interview covers.
See open roles