The instant difference between a Java process and thread is that a thread is a light weight process that does not require as many resources as a process but there are yet more and subtle differences exist. Let's see them as follows:
A process is a running instance of a program to which system allocates resources like CPU time and memory (separate heap, method area etc. which does not overlap with other process running on the system at the same time). In a big application there may be a set of cooperating processes communicating to each other in order to perform the desired functionality. Two processes communicate through well defined inter process communication mechanism, such as pipes, sockets and shared memory, if both processes are running on the same machine.
On the other hand threads exist within a process; every process has at least one thread. A thread is a light weight process that does not require as much resources as a process requires. Threads running inside a process, share the common set of resources among themselves which are allocated to the process (including the memory, the address space). All the threads share the same heap and method area (but individual stacks). All local variables are thread safe in Java because local variables are stored in each thread's own stack and each thread has its own stack created. Because threads share virtual address space, that makes inter thread communication between threads much cheaper than inter process communication between two independent processes.
Following is tabular comparison between threads and processes in Java.
Process | Thread |
---|---|
A process has separate virtual address space. Two processes running on the same system at the same time do not overlap each other. | Threads are entities within a process. All threads of a process share its virtual address space and system resources but they have their own stack created. |
Every process has its own data segment | All threads created by the process share the same data segment. |
Processes use inter process communication techniques to interact with other processes. | Threads do not need inter process communication techniques because they are not altogether separate address spaces. They share the same address space; therefore, they can directly communicate with other threads of the process. |
Process has no synchronization overhead in the same way a thread has. | Threads of a process share the same address space; therefore synchronizing the access to the shared data within the process's address space becomes very important. |
Child process creation within from a parent process requires duplication of the resources of parent process | Threads can be created quite easily and no duplication of resources is required. |
All threads belong to a process share common file descriptors, heap memory and other resource but each thread has its own exception handler and own stack in Java. Above mentioned differences are just the major differences between a process and a thread in Java.
Hope you have enjoyed reading the differences between thread and process in Java. Please do write us if you have any suggestion/comment or come across any error on this page. Thanks for reading!
Share this page on WhatsApp