Skip to Content
Course content

61: Parallel Collections

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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): Int that 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.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.