1. Parallelism vs. Concurrency

[THEORY]

[PREDICT]

Read the code carefully before you run it.

prediction.kt
import kotlinx.coroutines.*

fun main() = runBlocking {
    launch(Dispatchers.Default) { println("A") }
    launch(Dispatchers.Default) { println("B") }
}

Does this program guarantee that it prints A first and then B on every run?

[TASK]

A small order service should start its own coroutine for every incoming order, reporting which thread handles it. Complete the forEach loop: start a coroutine with launch(Dispatchers.Default) for each order, printing "$order runs on ${Thread.currentThread().name}".

task.kt
import kotlinx.coroutines.* fun main() = runBlocking { val orders = listOf("Order A", "Order B", "Order C") orders.forEach { order -> // TODO: launch(Dispatchers.Default) { ... } printing order and thread name } }

Before you get a hint: what have you tried, and where exactly are you stuck?

[COMPREHENSION CHECK]

1. How does the lesson distinguish parallelism and concurrency?

2. A program with two coroutines runs on a machine with only one CPU core. Is that possible?

3. According to this lesson, what was the motivation for programming in parallel or concurrently at all?