0
0
Kotlinprogramming~10 mins

Coroutines vs threads mental model in Kotlin - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Coroutines vs threads mental model
Start Main Thread
Launch Coroutine
Coroutine Suspends?
YesSave State, Free Thread
Thread Executes Other Tasks
Coroutine Runs
Coroutine Completes
Main Thread Continues
Shows how coroutines run on threads, can suspend without blocking, freeing threads to do other work, unlike threads which block while running.
Execution Sample
Kotlin
import kotlinx.coroutines.*

fun main() {
  println("Start")
  GlobalScope.launch {
    delay(1000)
    println("Coroutine done")
  }
  println("End")
  Thread.sleep(2000) // Keep JVM alive to see coroutine output
}
Starts main thread, launches coroutine that delays without blocking, main thread prints 'End' immediately.
Execution Table
StepActionThread StateCoroutine StateOutput
1Start main functionMain thread runningNo coroutine yetStart
2Launch coroutineMain thread runningCoroutine started, runningStart
3Print 'End'Main thread runningCoroutine running (delay started)Start End
4Coroutine delays (suspends)Main thread free for other tasksCoroutine suspended, state savedStart End
5After 1 second, coroutine resumesMain thread running coroutineCoroutine resumedStart End
6Coroutine prints 'Coroutine done'Main thread running coroutineCoroutine completesStart End Coroutine done
7Main function endsMain thread endsCoroutine endedStart End Coroutine done
💡 Coroutine completes after delay, main thread is never blocked during coroutine suspension.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6Final
Main threadRunning mainRunning main + launching coroutineRunning main (prints End)Running coroutineEnded
CoroutineNot startedStarted and runningSuspended (delay)Resumed and runningCompleted
Key Moments - 3 Insights
Why does the main thread print 'End' before 'Coroutine done'?
Because the coroutine suspends during delay (see step 4 in execution_table), freeing the main thread to continue and print 'End' immediately.
Does launching a coroutine create a new thread?
No, launching a coroutine does not create a new thread; it runs on existing threads and suspends without blocking them (see thread state in steps 2-4).
What happens to the thread when the coroutine suspends?
The thread is freed to do other work while the coroutine's state is saved (step 4), unlike a thread which would block and wait.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the state of the coroutine?
ARunning without interruption
BSuspended with state saved
CCompleted
DNot started yet
💡 Hint
Check the 'Coroutine State' column at step 4 in the execution_table.
At which step does the main thread print 'End'?
AStep 1
BStep 2
CStep 3
DStep 6
💡 Hint
Look at the 'Output' column and find when 'End' appears.
If the coroutine did not suspend during delay, what would happen to the main thread?
AMain thread would block and wait
BMain thread would create a new thread
CMain thread would continue immediately
DMain thread would end before coroutine starts
💡 Hint
Refer to the 'Thread State' column in steps 3 and 4 to understand thread behavior during suspension.
Concept Snapshot
Coroutines run on threads but can suspend without blocking them.
Threads block when running tasks; coroutines free threads during suspension.
Launching a coroutine does not create a new thread.
Coroutines save state on suspension and resume later.
This allows efficient multitasking without many threads.
Full Transcript
This visual trace shows how Kotlin coroutines differ from threads. The main thread starts and launches a coroutine. The coroutine begins running but suspends during a delay, freeing the main thread to continue and print 'End' immediately. Later, the coroutine resumes and prints 'Coroutine done'. Unlike threads, coroutines do not block threads when suspended, enabling efficient multitasking. The variable tracker shows main thread and coroutine states changing step by step. Key moments clarify common confusions about suspension and thread usage. The quiz tests understanding of coroutine states and thread behavior during execution.