DEV Community

Muhammad Wasi Naseer
Muhammad Wasi Naseer

Posted on

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

Top comments (0)