Friday, May 16, 2008

Processes vs Threads


Process: It's an execution environment which is complete and has a private set of basic run-time resources. Every process will has its own distinct address space, which is nothing but the allocated memory space to the particular process.


You can think of a process as a program or an application under execution, but it's not necessary that an application is composed of only one process. In fact, it's quite rare for any significant application.


Most of the operating systems support Inter Process Communication (IPC) resources to facilitate communication between processes. Example of such resources are Pipes and Sockets. Using these resources processes running on different system can also communicate quite easily and efficiently.


JVM normally runs as a single process in most of the implementations. ProcessBuilder is used by a Java application to create additional processes.


Thread: These are commonly explained as lightweight processes. They are lightweight because they share the same address space and other resources and hence need fewer resources while creation. All the threads spawned in a process will have the same address space, but they will have their own set of registers. Theads always exist within a process and they share other resources such as open files as well.


Both processes and Threads provide an execution environment, but threads are relatively easier to create as the operating system require fewer resources to create them. This makes them efficient, but this efficiency and ease comes with a price - threads are potentially problematic and the operating system as well as the programmer need to be very cautious while dealing with threads and their communication.


Java provides a multithreaded platform. Every Java application will have at least one thread - main thead, which starts the execution of the application. This thread can create, manage, and maintain as many threads as it wants while its execution.



Share/Save/Bookmark


No comments: