Beyond the Bootcamp: Threads, Parallelism, and Concurrency


As a 10,000 foot overview, we’ll be covering:

  • How we can use more than 1 thread to take advantage of computer hardware
  • Common issues from multi-threaded applications, race conditions and deadlocks
  • What does it mean for code to execute concurrently compared to in parallel?
  • Synchronous vs asynchronous code execution

There’s lots of talk about threads here. What’s a thread?

Why make use of more cores?

Depending on the type of application you’re building, a single-threaded application could suffice. However, there are situations in where a given problem might be too big for a single thread to handle. Perhaps you’re processing a large stream of data that requires you to perform some complicated long running calculation. In cases like these, we might want to consider making use of multiple cores. Time is expensive and we want answers and operations done faster!

A concrete example

Say we’re tasked to make a program that will take in a number and will return a boolean array where a boolean in each index of the array denotes whether or not that index is a prime number up until the number passed in.

Creating worker threads

Before we create worker threads to help speed up the program, we need to identify what needs to be sped up. Since we know that isPrime takes a long time to compute, we can partition the domain of numbers that need to be computed into equal chunks so each thread is only responsible for a subset of the numbers to be computed.

Pictorial view of the fork-join model

Wrapping up

In this edition, we set the stage for what this module will cover. We learned the basics about what a thread is and how we can leverage threads to speed up our programs. In the coming weeks we’ll be exploring the issues that arise from multi-threaded programs and what tools we have available to us to prevent these issues.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store