DEV Community

Muhammad Wasi Naseer
Muhammad Wasi Naseer

Posted on

1

Why do we need threads along with processes?

Let’s first define what a process is. A process is an independent isolated program to which an OS gives resources, file handles, or credentials. An OS can have many processes with its own address space, heap, or stack. When processes need to communicate, they need to use coarse-grained communication mechanisms such as a socket, shared memory, files, etc. Nothing is directly shared among processes.

A process can have many threads. A thread is like a lightweight process but with a major difference of sharing state. Threads share process-wide resources like files or memory which enables fine-grained communication. All threads within a process have their own stack, program counter, and local variables except heap. This sharing of state among multiple threads leads to concurrency problems which we’ll discuss in our upcoming posts.

Along with the benefits of multiprocessing(resource utilization, convenience, fairness), threads enable us to use the same process-wide resources without using any communication mechanisms.

REFERENCE

Topic#1.1: https://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay