From the course: Java Threads

Processes vs. threads - Java Tutorial

From the course: Java Threads

Processes vs. threads

- [Instructor] When talking about concurrency, two things come up, processes and threads. A process is an instance of a running program, that is, the execution of a program. For example, you can see different processes running at a given time on your computer for the programs that you are working with at that time. Processes are isolated. They use their own private memory space and do not share data with other processes. So processes may consume more resources, especially if they are heavy weight. A thread is a part of execution inside a program. You can even think of it as a subprocess. It executes a given piece of code. So in a program, there can be multiple threads of execution that can happen at the same time. This behavior helps programmers implement concurrency in applications. For example, imagine a program running to do a product lookup. One thread can bring data from the database while another thread handles the user interface. So the query execution can happen while the application is still responding to user input. Unlike processes, threads share data and memory with other threads within its own process. So threads consume fewer resources than processes and they're lightweight. In essence, a process is an isolated program execution. It does not share memory or data with other processes. The CPU and operating system can run many isolated processes at the same time, which we also call as programs. On the other hand, a thread is a code block that runs concurrently. It shares data and memory with other threads that are executing in parallel in its process. Threads also use fewer resources. A process or a program usually has multiple threads of execution or subprocess within them. So a process can spawn multiple threads within it and these threads will be doing their own jobs. For example, Thread1 could be rendering the UI. Thread2 could be fetching data from the database. Thread3 could be doing a file upload while the other threads are doing their own tasks. Switching between threads happens really fast. And in short, that's what's called multithreading. Within the CPU, there are processes running and subprocessors or threads running within them to make program execution more efficient.

Contents