1. Multithreading vs Parallelism
Multithreading: many, but not necessarily at the same time
Multithreading is when your program has multiple threads of execution. Each thread is like an independent line of work: one computes something, another waits for user input, a third saves data to a file. In Java you create threads via the Thread class, implement the Runnable interface, or use high-level tools such as ExecutorService (more on them in the next lecture).
BUT! Multithreading does not guarantee that your tasks actually run at the same time. It all depends on how many cores your CPU has. If there is only one core, threads simply switch between each other quickly — so quickly that it seems as if everything happens simultaneously. In reality, the processor executes only one thread at any given moment, and the others wait their turn.
Parallelism: when tasks truly run at the same time
Parallelism is when your code actually executes simultaneously on multiple CPU cores. If you have a modern machine with 4, 8, 16 cores, you can truly speed up large workloads by splitting them into independent parts and distributing them across the cores.
By analogy, multithreading is like having one cook who quickly switches between cooking borscht, frying cutlets, and chopping salad. Parallelism is when you have several cooks at once, and each is responsible for their own dish.
What’s the practical difference?
Multithreading is about convenience and responsiveness. You use multiple threads so the program doesn’t “freeze”: one thread waits on the network, another draws the UI, a third computes something. Everything feels concurrent, but not necessarily simultaneous.
Parallelism is about speed. Here multiple CPU cores actually execute different parts of the task at the same time to produce a result faster.
In other words: multithreading helps organize the work, while parallelism accelerates it.
Important:
Multithreading is needed whenever there are tasks that can be done independently.
Parallelism is needed when you want to speed up computation by truly distributing work across cores.
Example: processing a large array
Let’s imagine we have an array of 10 million numbers and we want to compute the sum of all elements.
Sequentially:
One thread iterates over the entire array and computes the sum. Simple and reliable, but slow.
Multithreaded (but on a single core):
You split the array into 4 parts, create 4 threads, and each computes its part. But if you have only one core, the threads will just take turns — there will be no speed-up, and the overhead of context switching may even slow the program down.
Parallel (on multiple cores):
You split the array into 4 parts, start 4 threads, and each thread actually runs on its own core. The final sum is assembled from 4 parts. This is truly faster — especially on large volumes of data.
On the other hand, implementing sequential array processing is very simple — you’ve written programs like this many times:
// Example: sequential array processing
int[] arr = new int[10_000_000];
// ... fill the array ...
long sum = 0;
for (int x : arr) {
sum += x;
}
System.out.println(sum);
The multithreaded and parallel versions are a bit more complex; we will cover them in the next lectures using modern tools.
2. Why parallelism is needed
Modern processors moved beyond a single core long ago. Even your smartphone most likely has at least four, and desktops and servers have eight, sixteen, thirty-two, or more. If an application can use all those cores, it can run many times faster.
In the past, CPU performance grew by increasing clock frequency — up until the mid‑2000s this worked. But frequency growth hit physical limits, and a new era began — multiprocessor and multicore systems. Now the programs that win are those that can efficiently distribute work across cores.
Where does parallelism truly speed things up?
- Big data processing: log analysis, statistics, aggregation — anything that can be split into independent chunks.
- Rendering, image and video processing: each pixel or fragment can be processed separately.
- Scientific computing, simulation: mathematical tasks, simulations, model training.
- Server applications: serving many clients concurrently.
- Reactive applications: when you need to respond quickly to many events without blocking the main thread.
When doesn’t parallelism help?
- If the task is small, the overhead of starting parallel work can outweigh the gains.
- If the task cannot be split into independent parts (for example, when each step depends on the previous one).
- When there are many shared resources (for example, the same file), and threads start interfering with each other.
3. Typical workloads for parallelism
Let’s look at the kinds of tasks most often distributed across cores.
Array computations
- Summation, finding max/min, computing statistics over a large array.
- Example: compute the average temperature from a million sensors.
Processing collections
- Filtering, sorting, transforming large lists (for example, processing an online store’s orders).
- Example: select all orders more expensive than 10,000 rubles and sort them by date.
Rendering and image processing
- Apply a filter to all pixels of an image (for example, make it grayscale).
- Each pixel can be processed independently — an ideal case for parallelism.
Data analysis, big data
- MapReduce, aggregation, computing statistics over huge data volumes.
- Example: process a year of logs to find anomalies.
Example: parallel sum
Suppose we have an array of 1 million numbers. You can split it into 4 parts and compute the sum of each part in a separate thread, then add up the results.
4. Problems and challenges of parallelism
Debugging complexity
When code runs in multiple threads, bugs may appear only in rare cases when threads “cross” in a particular way. Sometimes an error shows up once in 1,000 runs — and it’s very hard to catch.
Data races (race conditions)
If several threads modify the same variable or object at the same time, incorrect results are possible. For example, two threads increment a counter simultaneously, and the final value turns out smaller than expected.
Synchronization
To avoid races, you need to synchronize access to shared data — via the synchronized keyword, locks, atomic variables, and other tools. This complicates the code and can lead to other problems (for example, deadlock — mutual blocking of threads).
Load balancing
If you split a task into 4 parts and one turns out much heavier than the others, three threads will be done and idle while the fourth still works. In the end there’s no speed-up.
Overheads
Starting threads, switching between them, synchronization — all of this takes time. If the task is small, parallelism will only slow it down.
Table: comparing approaches
| Approach | When it runs fast | When it slows down | Example use case |
|---|---|---|---|
| Sequential (1 thread) | Small tasks, simple logic | Large data volumes | Processing 10 lines |
| Multithreading (on 1 core) | Asynchronous tasks (waiting for IO) | CPU-bound tasks on 1 core (limited by CPU computation) | Simultaneous file downloads |
| Parallelism (many cores) | Large, independent tasks | Small tasks, strong coupling | Processing a large array |
Visualization: what it looks like
// Sequential processing (1 thread)
[Task 1][Task 2][Task 3][Task 4]
// Multithreading on a single core (time slicing)
[Task 1] [Task 2] [Task 3] [Task 4]
(but only one actually runs at a time, the others wait)
// Parallelism on four cores
[Task 1] [Task 2] [Task 3] [Task 4]
(all run simultaneously)
5. Common mistakes when attempting parallelism
Mistake No. 1: Parallelizing everything indiscriminately. Many beginners think: “The more threads — the faster!” In reality, that’s not true. If there are few tasks or they are too simple, there is no gain — and sometimes the program even runs slower.
Mistake No. 2: Ignoring synchronization. If multiple threads work with the same data without synchronization, you’ll get data races, broken logic, and bugs that are hard to catch.
Mistake No. 3: Parallelism for its own sake. Parallelism is not an end in itself. It’s needed when there are real tasks that can be effectively split into independent parts.
Mistake No. 4: Ignoring the specifics of the task. Some tasks cannot be parallelized at all (for example, when step N+1 depends on the result of step N). In such cases, parallelism won’t give you an advantage.
Mistake No. 5: Ignoring the overhead. Starting threads, switching between them, collecting results — all this takes time. For small tasks, this time can exceed the time of the work itself.
GO TO FULL VERSION