In concurrent programming, there are two basic units of execution-
- Process
- Thread
A process, in a way, is an executing instance of an application. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. As example, running a Java IDE to write a Java program and visiting a web site at the same time.
A thread exists within a process and it is a path of execution within a process. This means that a single program can perform two or more tasks simultaneously. As example a word processor that is printing a document using a background thread and formatting text at the same time using another thread.
Process Vs Thread in Java
Let's see the differences between the thread and process in Java to have a clear idea what exactly is thread and what is process in Java.
- A process has a self-contained execution environment, Threads exist within a process - every process has at least one.
- Process are heavyweight tasks whereas threads are referred as lightweight processes as creating a new thread requires fewer resources than creating a new process.
- Each Process has its own separate address spaces, threads with in the same process share the process' resources, including memory and open files. This means that it's very easy to share data among threads, but it's also easy for the threads to bump on each other, which can lead to unpredictable scenarios like deadlock and race condition in multi-threading.
- Inter process communication is expensive whereas inter thread communication is inexpensive and can be achieved easily using wait and notify in Java.
- Context switching from one process to another is expensive; context switching between threads is generally less expensive than in processes.
- Threads are easier to create than processes as separate address space is not required for a thread.
That's all for this topic Difference Between Thread And Process in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!
Related Topics
You may also like-
Short and crisp, best post! I like it that it is not prolonged just for the sake of having more words in the post! Keep writing like this.
ReplyDelete