Tuesday, December 4, 2012

What is difference between Thread vs Process in Java



Thread and Process are two closely related term in multi-threading and main difference between Thread and Process in Java is that Threads are part of 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 most popular use of grep command in UNIX, Where identifier is unix 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 same process same memory space. Some linux command map Java thread with light weight process or lwp, e.g. if you use prstat command in Solaris, you can get how many light weight process or Thread a particular Java program is using.


Process vs Thread in Java

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


1) Both process and Thread are independent path 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 share it with other threads.

3) Threads are also refereed as task or light weight process (LWP) in operating system

4) Threads from same process can communicate with each other by using Programming language construct 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 Process which requires duplication of parent process.

6) All Threads which is part of same process share system resource like file descriptors , Heap Memory and other resource but each Thread has its own Exception handler and own stack in Java.

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

Other Java multi-threading tutorials you may like

2 comments:

  1. thanks for share..

    ReplyDelete
  2. 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

Java67 Headline Animator