Process vs Thread in Java

Process vs Thread in Java | In Java, program execution is often referred to as a process. It is an execution environment that consists of instructions, user-data, and system-data segments, as well as lots of other resources such as CPU, memory, address-space, disk, and network I/O acquired at runtime. 

A thread is a subset(part) of the process, it is the smallest unit/part of the process that can execute concurrently with other parts(threads) of the process. A thread is a lightweight process or sub-process. A thread simply executes instructions serially. The globally shared state amongst the threads of a process is visible and accessible to all the threads, and special attention needs to be paid when any thread tries to read or write to this global shared state. There are several constructs offered by various programming languages to guard and discipline access to this global state.

Assume to perform a task process1 and process2 are required then to perform that process1 and process2 multiple sub-process are required. Those sub-processes are nothing but a thread.

Thread vs Process in Java

When we run a Java application then one process is created having two default threads:- main thread and garbage collector thread.

Note:- Both process and Thread are managed by the operating system.

Process vs Thread Java Language

Now let us see the differences between Process and Thread in Java Programming Language in tabular form.

ProcessThread
Program execution is often referred to as a process. A thread is a subset(part) of the process.
It contains multiple threads.It is the smallest part of the process.
Also referred to as a heavyweight task.Also referred to as a lightweight process.
A process has its own address space.It uses the process’s address space and shares it with the other threads of that process.
Processes don’t share any resources among themselves.Threads of a process can share the resources allocated to that particular process, including memory address space. 
Communication between processes can be performed by using inter-process communication.Communication between threads of the same process can be performed by using methods like wait(), notify(), notifyAll().
Process-based multitasking allows a computer to run two or more programs concurrently.Thread-based multitasking allows a single program to run two or more threads concurrently.
A process has its own address space, global variables, signal handlers, open files, child processes, accounting information.A thread has its own register, state, stack, program counter.
We do not require synchronization in case of process.Threads require synchronization to avoid unexpected scenarios.
To create a new process (sibling process) we need to create a duplicate of the parent process.It is easy to create new threads by calling the thread’s start method.
A process does not have control over the sibling process, it has control over its child processes only.Threads have control over the other threads of the same process. 
Context switching between processes takes more time.Context switching between threads takes less time.

Process and Thread Context Switching

Thread Context Switching:- Transferring control from one thread to another thread is called thread context switching or control jumping.

Process Context Switching:- Transferring control from one process to another thread is called process context switching or control jumping.

Thread vs Process Context Switching

Both thread context switching and process context switching is done based on some scheduling algorithm.

The thread context switching takes always less time compared to process context switching. 

We can understand it as in a school, switching from one classroom to another classroom takes less time compared to moving from one school to another school.

What is the need of context switching between thread and process?
Our computer CPU is limited and it can’t give the entire time to complete only one task. It has to divide time for multiple processes and threads. In order to utilize CPU resources effectively for multiple processes/threads the context switching is required.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *