Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 943

Difference Between Thread And Process in Java

$
0
0

In concurrent programming, there are two basic units of execution-

  • Process
  • Thread

Both of these units of executions differ in the way they use the execution environment. In this post we'll see the difference between thread and process in Java.

A process is an independent executing instance of an application. Each process has its own memory space and resources. Thus, process-based multitasking is the feature that allows your computer to run two or more programs concurrently. For example, running a Java IDE to write code while simultaneously browsing a website involves two separate processes.

A thread, on the other hand, is a lightweight unit of execution that exists within a process. Multiple threads can run inside the same process, sharing memory and resources. This means that a single application can perform two or more tasks simultaneously. For 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.

  1. Execution Environment
    A process has its own self-contained execution environment, while threads exist within a process. Every process has at least one thread.
  2. Resource Usage
    Process are heavyweight tasks; creating a new process requires significant resources. Threads are referred as lightweight processes as creating a new thread requires fewer resources than creating a new process.
  3. Memory Management
    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.
  4. Communication
    Inter process communication is costly whereas inter-thread communication is inexpensive and can be achieved easily using wait and notify in Java.
  5. Context Switching
    Switching between processes is expensive. Thread context switching is faster and more efficient.
  6. Ease of Creation
    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

  1. Creating Thread in Java
  2. Thread States (Thread Life Cycle) in Java Multi-Threading
  3. Deadlock in Java Multi-Threading
  4. Why wait(), notify() And notifyAll() Must be Called Inside a Synchronized Method or Block
  5. Java Multithreading Interview Questions And Answers

You may also like-

  1. Java ReentrantLock With Examples
  2. Fail-Fast Vs Fail-Safe Iterator in Java
  3. Difference Between Checked And Unchecked Exceptions in Java
  4. Creating Custom Exception Class in Java
  5. Interface Static Methods in Java
  6. Constructor Chaining in Java
  7. Covariant Return Type in Java
  8. Lambda Expressions in Java 8

Viewing all articles
Browse latest Browse all 943

Trending Articles