A Job represents a cancellable task or coroutine. It controls the coroutine's lifecycle, allowing you to start, cancel, or check its status.
Click to reveal answer
intermediate
Name the main states in a Job's lifecycle.
The main states are:
New: Created but not started.
Active: Running or suspended.
Completing: Finishing work.
Cancelled: Stopped before completion.
Completed: Finished successfully.
Click to reveal answer
beginner
How do you cancel a Job in Kotlin?
You call the cancel() function on the Job instance. This requests cancellation, and the coroutine cooperatively stops if it checks for cancellation.
Click to reveal answer
intermediate
What happens if a coroutine ignores cancellation?
If a coroutine does not check for cancellation (e.g., by calling yield() or checking isActive), it may continue running even after cancel() is called, delaying cancellation.
Click to reveal answer
intermediate
Explain cooperative cancellation in Kotlin coroutines.
Cooperative cancellation means the coroutine must regularly check if it is cancelled and stop work accordingly. This is done by checking isActive or using cancellable suspending functions.
Click to reveal answer
Which function is used to stop a running Job in Kotlin?
Acancel()
Bstop()
Cterminate()
Dend()
✗ Incorrect
The cancel() function requests cancellation of a Job.
What state is a Job in when it has finished successfully?
AActive
BCancelled
CNew
DCompleted
✗ Incorrect
A Job is Completed when it finishes its work successfully.
What must a coroutine do to respond to cancellation?
ACheck for cancellation cooperatively
BIgnore cancellation requests
CCall cancel() on itself
DRun on a separate thread
✗ Incorrect
Coroutines must cooperatively check for cancellation to stop execution.
Which of these is NOT a Job lifecycle state?
ANew
BActive
CPaused
DCancelled
✗ Incorrect
There is no Paused state in Job lifecycle.
What happens if you call cancel() on a Job but the coroutine never checks for cancellation?
AThe coroutine stops immediately
BThe coroutine ignores cancellation and keeps running
CAn error is thrown
DThe Job restarts automatically
✗ Incorrect
Without cooperative checks, the coroutine keeps running despite cancellation requests.
Describe the lifecycle of a Kotlin Job and how cancellation affects it.
Think about how a Job moves from new to completed or cancelled.
You got /4 concepts.
Explain why cooperative cancellation is important in Kotlin coroutines.
Consider what happens if a coroutine ignores cancellation.
You got /4 concepts.
Practice
(1/5)
1. Which of the following best describes the lifecycle of a Kotlin Job?
easy
A. A job is always active and never completes.
B. A job starts cancelled and then becomes active.
C. A job starts active, can be cancelled, and then completes.
D. A job completes before it starts.
Solution
Step 1: Understand Job lifecycle states
A Kotlin Job begins in an active state, meaning it is running or ready to run.
Step 2: Recognize cancellation and completion
A Job can be cancelled to stop its work, and after finishing or cancellation, it reaches a completed state.
Final Answer:
A job starts active, can be cancelled, and then completes. -> Option C
Quick Check:
Job lifecycle = active -> cancelled -> completed [OK]
Hint: Jobs start active, then cancel or complete [OK]
Common Mistakes:
Thinking jobs start cancelled
Assuming jobs never complete
Confusing job states order
2. Which Kotlin code snippet correctly cancels a job and waits for its completion?
easy
A. job.cancelAndJoin()
B. job.cancel()
C. job.start()
D. job.join()
Solution
Step 1: Identify cancellation with waiting
The method cancelAndJoin() cancels the job and suspends until it finishes.
Step 2: Differentiate from other methods
cancel() only requests cancellation without waiting; join() waits but doesn't cancel; start() begins a job.
Final Answer:
job.cancelAndJoin() -> Option A
Quick Check:
Cancel and wait = cancelAndJoin() [OK]
Hint: Use cancelAndJoin() to cancel and wait [OK]
Common Mistakes:
Using cancel() without waiting
Confusing join() with cancel
Calling start() to cancel
3. What will be the output of this Kotlin coroutine code?
val job = CoroutineScope(Dispatchers.Default).launch {
repeat(3) { i ->
println("Working on $i...")
delay(100)
}
}
job.cancel()
Thread.sleep(500)
medium
A. Prints all three "Working on" lines
B. Prints some "Working on" lines before stopping
C. Throws a cancellation exception
D. Prints no output
Solution
Step 1: Analyze job launch and cancellation
The job starts and prints "Working on 0...", then delays. The cancel() is called immediately after launch.
Step 2: Understand cancellation effect
Because cancellation happens quickly, some iterations may run before the job stops, so partial output appears.
Final Answer:
Prints some "Working on" lines before stopping -> Option B
Quick Check:
Partial output due to quick cancel [OK]
Hint: Cancel stops job but may print partial output [OK]
Common Mistakes:
Expecting all lines printed
Expecting no output at all
Assuming exception is printed
4. Identify the error in this Kotlin coroutine cancellation code: