Thread vs Process – Understanding Difference

In our previous article, we discussed how JVM starts as a process and spawns many threads. The question thread vs process becomes very obvious.

In this article we will discuss:

  1. Relationship between thread and process
  2. Difference between thread and process

So, stay tuned!!

Current article is part of our series on multithreading in java.

Relationship Between Thread And Process

A process can have multiple threads. All the threads of a process lives and dies within that process itself.

Also, when we are talking specifically in the context of Java, it is good to know that we are talking about JVM process only.

So, if you execute java command twice, it will create two independent instances of JVM processes.

It does not matter if it is executing same or different program. What matters is that we are executing it twice and that’s it.

java YourProgram
Java AnotherProgram

These independent JVM processes will have their own set of threads. So, threads of one JVM process is completely independent of threads of another JVM process.

On the other hand, threads within a single JVM might be related to each other, depending upon how we have modeled our threads to work.

Now, when we have understood the relationship between process and thread, let’s now proceed to the differences.

Difference Between Thread And Process

Program vs Process

Even before talking difference between process vs thread, let’s first talk about program vs process.

Although it is very obvious to most of the Java developers, it sometimes be confusing to the newcomers specially when they are directly coming from academics.

So, let’s make this point clear.

When you write a Java class like:, it is a program. Right now, it is just residing in your system’s hard disk.

But when you execute, java AddTwoNumbers:

First, operating system starts a JVM process from the java command program(part of JDK).

And then, this JVM process executes our program AddTwoNumbers.

Definition: A process is a program in execution.

NOW, let’s understand:

Process vs Thread

A process works as an execution environment wherein we can execute our program.A thread is the smallest unit of execution, within the scope of a process.
Process has access to operating system resources.Thread uses OS resources provided by process.
Process is costly to create and maintain in terms of CPU and memory.Thread is comparatively not costly in terms of CPU and memory. Hence, it is also called as Lightweight process.
We use inter-process communication mechanisms to talk between two processes. e.g. SocketsWe use inter-thread communication mechanism to talk between two threads. e.g. wait and notify
As two processes are always independent to each other, there is no risk of memory sharing.As threads within a process works in same memory space, there is risk present of memory sharing.

Key Points

  1. Each execution of java command creates a new JVM process.
  2. Each of these JVM process has their own set of threads.
  3. Threads within a JVM process is totally independent of threads within any other JVM process.
  4. Threads within a single JVM process might be related to or dependent on each other.

What to read next?

Now that you have understood: What is a process, What is a thread, their relationship and differences, let’s now understand What is Concurrency, it’s relationship with single core and multi-core system.

Go to: Understanding Concurrency

Leave a Comment