Multithreading in Java Part 1 - Process vs Thread

Daniel Rendox - Apr 2 '23 - - Dev Community

Intro

Multithreading is an important programming concept that allows multiple tasks to be executed simultaneously within a single process. It can significantly improve the performance of applications, particularly those that require a lot of processing power or need to perform multiple tasks at the same time. While designing multithreaded applications can be complex, understanding this concept is valuable for any software developer.

My series of articles are intended to explain essential things about multithreading in Java in a simple way.

In this article, we will explore differences between Processes and Threads. Why is this important? Well, concurrency can be achieved in numerous ways, including multiprocessing and multithreading, and they are each used differently for distinct purposes. Therefore, understanding the differences is crucial. Since we will be focusing on multithreading, and these two topics share similarities and can be easily confused, I will highlight some key differences. And you maybe want to know how all of these looks for a computer. Let's dive into it.

Computer's interpretation of processes and threads

A process is a small program within an app, whereas a thread is a small program within a process. Your CPU (central processing unit) is responsible for executing them. A single CPU can run several threads but only one process at a time. Meanwhile, a CPU has several cores, each of them is responsible for running a single thread.

A CPU is responsible for a process, a CPU's core is responsible for a thread

For example, I have 8 CPUs and each of them contains 4 cores, so my computer can run 8 processes and 32 threads concurrently. To check how many you have open Task Manager, Performance tab.

Task Manager, Performance tab, the numbers of Cores and Logical processors can be seen right there

Processes

An example of a process can be a single web application run by Opera browser. Since there are lots of web apps opened on different tabs running simultaneously, it’s appropriate for a browser to split them into different processes.

Task Manager, Performance tab, here you can see different apps running and some of them have expandable lists of processes like Opera that is running 35 of them

Although I have 17 tabs opened in my web browser, the number of processes is 35. How is my system not overloaded? The answer is in multitasking. Each CPU can switch rapidly between running processes. When some tasks get done, it switches to another one and this is done so fast that it manages to run all the applications and processes. That's why CPUs actually appear to run several processes at the same time, but in fact they don't.

Threads

A game is a good example of multithreading applications. There are lots of things that should be done at the same time in games: audio, different characters’ behavior, weather, user interface etc.

RDR 2, Guys shooting

When to use threads and when — processes

Threads are typically used when the task involves a lot of I/O or synchronization with other threads or processes. For example, a web server that needs to handle multiple requests concurrently can use a thread pool to process each request in a separate thread. This allows the server to handle multiple requests simultaneously and avoid blocking while waiting for I/O operations to complete.

On the other hand, processes are typically used when the task involves a lot of CPU-bound processing or when there are multiple CPUs or cores available to execute the task in parallel. For example, a video encoding application that needs to encode multiple videos concurrently can use multiple processes to encode each video in a separate process. This allows the application to take advantage of the available CPUs or cores and encode multiple videos simultaneously.

Take into account

To switch between two processes, the CPU has to terminate the current process, save it, as it's not finished, and then start another one. From the other hand threads don’t require such operations, because they share memory with each other within the same process. So for a number of reasons, threads are considered as lighter weight operations than processes.

What's next?

Did you enjoy this article? I hope now you can answer the question "What's the difference between threads and processes?" In the next article I will cover different ways to create a thread in Java. Do you have any questions? Feel free to ask in the comments and don't forget to like the article.

. . . . . . . . . . . . . . . . . . . .