Free preview.You're sampling one lesson — enroll free to unlock all 8 lessons and track your progress.
Enroll free
lesson

Concurrency vs. Parallelism

Concurrency vs. Parallelism

In this lesson — part of Concurrency Basics — you'll learn concurrency vs. parallelism in Scala and why it matters in real work.

Why it matters

Modern programs wait on networks and disks — async lets them stay responsive.

Key ideas

  • Blocking vs. non-blocking
  • Callbacks, promises, async/await
  • Concurrency vs. parallelism
  • Error handling in async code

In practice

Here's how it looks in idiomatic Scala:

import scala.concurrent.{Future, Await}
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.duration.*

val f: Future[Int] = Future { Thread.sleep(50); 21 * 2 }

// compose asynchronously, never blocking
val g = f.map(_ + 1).recover { case _ => 0 }
g.foreach(println)             // callback when ready

Await.result(g, 1.second)      // block only at the edge (e.g. in main/tests)

Scala note: A Future starts running the instant it's created (given an ExecutionContext), unlike lazy effect types such as cats-effect IO, so map/flatMap chain results without ever calling Await except at program boundaries.

Try it yourself

Exercise: In Scala, fetch two things concurrently and combine the results.

Recap

You now understand concurrency vs. parallelism and can apply it in Scala. Mark this lesson complete and continue to the next one.