🔴 Scala Advanced

What is the Scala concurrency model compared to Java?

Answer

Scala offers several concurrency models beyond Java's raw threads and locks. Futures: the simplest async model — callbacks on thread pool tasks. Better than Java callbacks but not purely functional. Akka Actors: message-passing concurrency with shared-nothing isolation. Avoids synchronization entirely for most patterns. Cats Effect/ZIO Fibers: functional green threads — millions of fibers multiplexed over a small thread pool. Automatic cancellation, resource safety, and structured concurrency (parent fibers manage child lifecycle). Parallel collections: list.par.map(f) — easy but crude parallelism for CPU-bound tasks. Java Virtual Threads (Loom): available in Scala on JDK 21+ — the JVM now natively supports lightweight threads. Compared to Java, Scala's functional effect systems (Cats Effect, ZIO) provide structured concurrency guarantees that Java lacks — leaked fibers, resource leaks on cancellation, and error propagation are handled automatically by the runtime rather than being developer responsibilities.