Sitemap
Hüseyin Can Ercan

Hüseyin Can Ercan

Senior software engineer with both enterprise and startup experience.

Multithreading Perspective — Java

5 min readDec 5, 2023

--

Press enter or click to view image in full size
Photo by Bozhin Karaivanov on Unsplash

Multithreading is a relatively complex topic in programming; even some languages don’t support it all. In this article, we will discuss the multithreading perspective in Java and Spring Boot.

What is multithreading?

Multithreading is a way to execute multiple tasks concurrently. In some cases, we may need more than one ongoing task to be executed at the same time.

Think about operating systems; you can play games, listen to music, and browse the web at the same time. All these tasks are executed concurrently on a single machine. And people have invested a lot of effort to make this happen.

Multithreading is not easy; we don’t use it often, but it is very important to understand the concept. It could enable very powerful or critical features in our applications.

A Simple Example

Imagine you need to paint a room with four walls. You are painting the walls by yourself; this is a single-threaded solution. The task is going to take 4 hours. If you have 3 friends, each person can paint a wall concurrently, and the job is going to take 1 hour.

This is a very simple example; in the real world, things are much more complicated.

Do you have enough paint brushes, paint buckets, and ladders? Do you have enough space for 4 people to paint at the same time?

Things will get complicated very quickly. A lot of things will emerge when we are trying to make things concurrent.

Do we need multithreading in a web app?

Spring Boot is a framework that helps us build web applications with Java. It is a powerful tool. You can easily create API endpoints, connect to databases, etc.

In Spring Boot, every HTTP request is handled by a thread. As developers, we don’t need to worry about it; Spring Boot will handle it for us.

But sometimes we need to do some heavy tasks. Imagine we need to execute a long calculation or make lots of requests to different servers. These tasks are going to take some time; we could face time problems with single thread implementation because it will take too long.

Firstly, this situation mostly indicates an architectural problem; we should not do heavy tasks in the same thread that handles the request. But sometimes we have to do it. Imagine you are out of the cloud, and you don’t have event-driven architecture.

Again let’s imagine we are doing something not too small nor too big. We are checking five different API endpoints to create a report. Each API endpoint takes 1 second to respond and 10 seconds to get a timeout, and we need to wait for all of them to finish.

The time cost will be:

If we execute them single-threaded: min 5 seconds, max 50 seconds

If we execute them multi-threaded: min 1 second, max 10 seconds

Basic implementation examples are given in the example above.

Get Hüseyin Can Ercan’s stories in your inbox

Join Medium for free to get updates from this writer.

Sample with executor service:

public class Main {
public static void main(String[] args) {
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<String>> futures = new ArrayList<>();

for (int i = 0; i < 5; i++) {
futures.add(executorService.submit( () -> new Task(i).call()));
}

for (Future<String> future : futures) {
try {
System.out.println(future.get());
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}

executorService.shutdown();
}
}

Same code with a parallel stream:

public class Main {
public static void main(String[] args) {
List<String> results = IntStream.range(0, 5)
.parallel()
.mapToObj(i -> new Task(i).call())
.collect(Collectors.toList());

results.forEach(System.out::println);
}
}

We are running a pure function 5 times concurrently. In the real world, things could be more complicated.

What happens if we add results to a list?

public class Main {
public static void main(String[] args) {
List<String> results = new ArrayList<>();

IntStream.range(0, 5)
.parallel()
.mapToObj(i -> new Task(i).call())
.forEach(results::add);

results.forEach(System.out::println);
}
}

If you execute this code 10,000 times, it could work just fine, but if you execute it 100,000 times, you can get a concurrent modification exception. This is the danger of multithreading. When you are doing something concurrently, you need to be very careful about it.

If we remember our room painting example, 5 people are painting the walls concurrently, but 2 of them tried to use the same paint bucket at the exact same time. This could happen with a low probability, but it could happen and cause problems.

Java will handle the scheduling for us; it will look for the best time to execute the tasks. But the scheduler doesn’t care who is using what; it just executes the tasks. We need to prevent similar situations.

We need to write thread-safe code, and we need to make sure that our code is safe to be executed concurrently.

Use collectors to collect results.

public class Main {
public static void main(String[] args) {
List<String> results = IntStream.range(0, 5)
.parallel()
.mapToObj(i -> new Task(i).call())
.collect(Collectors.toList());

results.forEach(System.out::println);
}
}

In this implementation, we are not sharing a common resource; we are just collecting the results of a final product. This is a thread-safe implementation.

Thread safety is the key to multithreading. If we cannot construct a thread-safe implementation, we cannot use multithreading.

We have some tools to make our implementation thread safe. Java synchronisation blocks or object locks could make a code block accessible to one concurrent thread and prevent concurrency exceptions. Specially shared objects could be secured with them; as an alternative, we may use thread-safe versions of collections.

But this safety comes with a cost; if we lock a resource too long, it will create a bottleneck, and we cannot see the full benefits of concurrency. The logical thing is to lock down the critical resource for the mandatory operations.

Imagine we have 3 steps:

  • Remove a value from a list
  • Costly calculation
  • Write result to another list

If we lock the resource and result lists in a single block and make the calculation within this block, even if we try to run it with multiple threads, the result is not going to be different than single-threaded execution. There is going to be only one active thread, and others are going to wait.

We need to design a better approach. We should apply a smaller lock to the resource list. Calculation should be a pure function; it cannot use any shared variable. After the calculation, we should lock the result list and enter our individual result.

This way, we can get the benefits of multi-threading for our costly calculations without facing a bottleneck.

Mentioned implementation concept:

public class Main {
public static void main(String[] args) {
List<String> results = new ArrayList<>();
List<Integer> resources = new ArrayList<>(List.of(1, 2, 3, 4, 5));

IntStream.range(0, 5)
.parallel()
.forEach(i -> {
int resource;
synchronized (resources) {
resource = resources.remove(0);
}

String result = new Task(resource).call();

synchronized (results) {
results.add(result);
}
});

results.forEach(System.out::println);
}
}

The word “thread” in this context is derived from the analogy of a thread in a piece of cloth. Just as multiple threads can be woven together to create a fabric, multiple threads can be executed concurrently to create a complex program.

--

--

Hüseyin Can Ercan

Written by Hüseyin Can Ercan

Senior software engineer with both enterprise and startup experience.