Scala
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Scala
-
Section 4: Functional Scala
-
Section 5: Collections in Depth
-
Section 6: Type System
-
Section 7: Concurrency and Ecosystem
-
Section 8: Practical Projects
-
Section 9: Interview Practice
-
Section 10: Data Structures and Algorithms in Scala
-
Section 11: More Practice Exercises
-
Section 12: Advanced Functional Patterns
-
Section 13: More Ecosystem
-
Section 14: Scala Collections Library Deep Dive
-
Section 15: Scala Standard Library Deep Dive
-
Section 16: Akka Ecosystem Deep Dive
-
Section 17: Cats and Cats Effect Deep Dive
-
Section 18: Play Framework Deep Dive
-
Section 19: Apache Spark with Scala Deep Dive
-
Section 20: Scala Build Tools Deep Dive
-
Section 21: Scala 3 Specific Features
-
90: Union and Intersection Types
-
Section 22: Scala Testing Deep Dive
-
Section 23: Functional Domain Modeling
-
Section 24: More Data Structures and Algorithms in Scala
-
Section 25: Scala for Data Engineering
-
Section 26: More Practical Projects
-
Section 27: More Interview and Review
-
Section 28: ZIO Ecosystem Deep Dive
-
Section 29: Scala for Machine Learning
-
Section 30: Scala Microservices Architecture
-
Section 31: Scala Type System Deep Dive
-
Section 32: More Practice and Drills
-
Section 33: Scala Performance Deep Dive
-
Section 34: Scala Ecosystem Tooling
-
Section 35: Scala for Reactive Systems
-
Section 36: More Real-World Case Studies
-
Section 37: Scala for Financial Systems
-
Section 38: Scala GraphQL and gRPC
-
Section 39: More Final Projects
-
Section 40: More Interview and Final Review
-
Section 41: Scala for Streaming Data
-
Section 42: Scala Security Practices
-
Section 43: More Language Deep Dive
-
Section 44: Scala Command-Line Tools
-
Section 45: Scala Documentation and Style
-
Section 46: Scala Dependency Management
-
Section 47: More Practical Backend Patterns
-
Section 48: Scala for Event-Driven Architecture
-
Section 49: More Practice Drills Round 2
-
Section 50: Scala Compiler Deep Dive
-
Section 51: Scala for Web Frontends
-
Section 52: More Data Engineering Practice
-
Section 53: Scala Observability
-
Section 54: More Advanced Practice Projects
-
Section 55: Scala for Legacy Java Integration
-
Section 56: More Testing Practice
-
Section 57: Final Mastery Review
-
Section 58: Scala History and Ecosystem Context
-
Section 59: More Concurrency Patterns
-
Section 60: Scala for Configuration Management
-
Section 61: More Domain Modeling Practice
61: Parallel Collections
You've probably noticed that most of the collection methods we've used so far—map, filter, fold—run on a single thread. For small lists, that's perfect. But when you're dealing with millions of records or computationally expensive operations, you're leaving a lot of performance on the table if you have a multi-core processor. That's where parallel collections come in.
I want to show you how this works by simulating a heavy workload. Let's imagine we're building a tool that processes a batch of high-resolution images to calculate a "complexity score" based on some dummy mathematical operations. In a real app, this would be CPU-intensive image analysis; here, we'll just use a loop that burns some cycles.
Starting with a slow, sequential loop
First, let's set up a basic sequential version. I'll create a list of "images" (just IDs for now) and a function that simulates a heavy task by calling Thread.sleep and doing some math.
def calculateComplexity(imageId: Int): Int = {
// Simulate heavy CPU work
Thread.sleep(10)
imageId * 31 % 100
}
val images = (1 to 1000).toList
val startTime = System.currentTimeMillis()
val results = images.map(calculateComplexity)
val endTime = System.currentTimeMillis()
println(s"Processed ${results.size} images in ${endTime - startTime}ms")
Running this is painfully slow. It takes about 10 seconds because it's processing every single image one after another. If you have 8 cores on your machine, 7 of them are basically taking a nap while one does all the work.
Flipping the switch to parallel
The beauty of Scala's parallel collections is that the API is almost identical to the sequential one. If you're using Scala 2.13+, you'll need the scala-parallel-collections module in your build, but once that's there, you just call .par on your collection.
import scala.collection.parallel.CollectionConverters._
val images = (1 to 1000).toList
val startTime = System.currentTimeMillis()
// Convert to a parallel collection
val results = images.par.map(calculateComplexity)
val endTime = System.currentTimeMillis()
println(s"Processed ${results.size} images in ${endTime - startTime}ms")
Now, the speedup is dramatic. On my machine, it drops from 10 seconds to about 1.5 seconds. Scala is splitting the list into chunks and distributing those chunks across the available cores of the ForkJoin pool. It's a massive win for very little code change.
The classic race condition trap
Now, here is where I usually trip up when I'm rushing a project. I decided I wanted to keep track of how many "complex" images (score > 50) we found while the processing was happening. I tried to use a simple mutable variable outside the map.
var complexCount = 0 // Shared mutable state!
val results = images.par.map { id =>
val score = calculateComplexity(id)
if (score > 50) {
complexCount += 1 // This is the mistake
}
score
}
println(s"Found $complexCount complex images")
When I run this, the complexCount is almost always wrong. It might say 342 instead of 480. Why? Because complexCount += 1 isn't an atomic operation. Multiple threads are reading the current value, adding one, and writing it back at the exact same time, overwriting each other's work. This is a textbook race condition.
Cleaning up the state
The fix is to stop thinking imperatively. Instead of trying to update a global counter from inside a parallel process, I should let the parallel collection do what it's best at: aggregating results. I'll use a filter and then check the size, or use a count operation.
val results = images.par.map(calculateComplexity)
// Count the results after the parallel processing is done
val complexCount = results.count(_ > 50)
println(s"Found $complexCount complex images")
By moving the counting logic to its own step (which is also parallelized by the .count method), I've eliminated the shared mutable state. The code is now thread-safe, and it's still blazing fast. Remember: the moment you reach for a var inside a .par block, stop and ask yourself if you can use a functional transformation instead.
📋 Practical Task
Exercise: Parallelizing a Large-Scale Log Analysis Tool
You are given a list of 50,000 simulated log entries (strings). Some logs are marked as "ERROR" and some as "INFO". Each log entry needs to be "parsed" using a function that simulates a delay (use Thread.sleep(1)) and returns the length of the error message if it's an error, or 0 otherwise.
Your Goal:
- Create a list of 50,000 strings (mix of
"INFO: all good"and"ERROR: something broke"). - Implement a function
parseLog(log: String): Intthat sleeps for 1ms and returns the length of the string if it contains "ERROR". - Process this list using a parallel collection to find the total sum of all error message lengths.
- Ensure you do NOT use any shared mutable variables (like a global
var sum = 0) to accumulate the total; use a parallel reduction or aggregation method instead. - Print the total sum and the time it took to execute.
There are no comments for now.