Saturday, December 10, 2022

Difference between Thread vs Process in Java? Example

Thread and Process are two closely related terms in multi-threading and the main difference between Thread and Process in Java is that Threads are part of the process. i.e. one process can spawn multiple Threads. If you run a Java program in UNIX based system e.g. Linux and if that program creates 10 Threads, it still one process and you can find that by using ps -ef | grep identifier command which is one of the most popular use of grep command in UNIX, Where identifier is UNIX the text which can be used as regular expression to find that Java process.


Another major difference between Process and Thread is that each process has its own separate memory space but Threads from the same process same memory space.

Some Linux command map Java thread with lightweight process or lwp, like if you use the prstat command in Solaris, you can get how many lightweight processes or Threads a particular Java program is using.


Process vs Thread in Java

In this section, we will see some more differences between Process vs Thread in Java to get a clear idea about What is a process and What is Thread in Java :


1. Both process and Thread are independent paths of execution but one process can have multiple Threads.

2. Every process has its own memory space, executable code, and a unique process identifier (PID) while every thread has its own stack in Java but it uses process main memory and shares it with other threads.

3. Threads are also refereed as task or lightweight process (LWP) in operating system


4. Threads from the same process can communicate with each other by using Programming language constructs like wait and notify in Java and much simpler than inter-process communication.

5. Another difference between Process and Thread in Java is that it's How Thread and process are created. It's easy to create Thread as compared to A process which requires duplication of the parent process.

6. All Threads which is part of same process share system resource like a file descriptors, Heap Memory and other resources but each Thread have its own Exception handler and own stack in Java. You can further check these Java Multithreading courses sot learn more bout processes and threads in Java. 

Difference between Thread vs Process in Java? Example

These were some of the fundamental differences between Process and Thread in Java. Whenever you talk about Process vs Thread, just keep in mind that one process can spawn multiple Threads and share the same memory in Java. Each thread has its own stack.


Other Java multi-threading tutorials you may like

Thanks for reading this article of far. If you find this Java Thread vs Porocess tutorial useful then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 

P. S. - If you want to learn Java multithreading and concurrency and looking for free courses to start with then you can also check out these best free Java Multithreading courses online. It is a good free course to learn  Java Concurrency as well.

5 comments:

  1. Java supports inter thread communication using wait notify, signal and await but there is no direct way to communicate between two processes in Java. Of course you can use RMI to talk between two application running but it's more of networking than inter process communication. I guess only way to achieve true inter process communicataion is by using memory mapped IO.

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.