Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are threads called lightweight processes?

A thread is "lightweight" because most of the overhead has already been accomplished through the creation of its process.

I found this in one of the tutorials.

Can somebody elaborate what it exactly means?

like image 609
Vijay Avatar asked Feb 15 '10 16:02

Vijay


People also ask

Which is called lightweight process?

Lightweight processes (LWPs) bridge the user level and the kernel level. Each process contains one or more LWP, each of which runs one or more user threads. (See Figure 1-1.) The creation of a thread usually involves just the creation of some user context, but not the creation of an LWP.

Why threads in Java are known as lightweight threads?

@Maduri light weight in this context means that there is generally a lot less overhead involved in creating a thread or switching execution between threads than with separate processes. The exact amount of overhead in either case heavily depends on the operating system.

What is thread differentiate between process and thread why thread is called Light weight?

A thread is a lightweight process that can be managed independently by a scheduler. It improves the application performance using parallelism. A thread shares information like data segment, code segment, files etc. with its peer threads while it contains its own registers, stack, counter etc.

Why thread is not a lightweight process in Java?

Threads are lightweight process only if threads of same process are executing concurrently. But if threads of different processes are executing concurrently then threads are heavy weight process.


1 Answers

The claim that threads are "lightweight" is - depending on the platform - not necessarily reliable.

An operating system thread has to support the execution of native code, e.g. written in C. So it has to provide a decent-sized stack, usually measured in megabytes. So if you started 1000 threads (perhaps in an attempt to support 1000 simultaneous connections to your server) you would have a memory requirement of 1 GB in your process before you even start to do any real work.

This is a real problem in highly scalable servers, so they don't use threads as if they were lightweight at all. They treat them as heavyweight resources. They might instead create a limited number of threads in a pool, and let them take work items from a queue.

As this means that the threads are long-lived and small in number, it might be better to use processes instead. That way you get address space isolation and there isn't really an issue with running out of resources.

In summary: be wary of "marketing" claims made on behalf of threads. Parallel processing is great (increasingly it's going to be essential), but threads are only one way of achieving it.

like image 191
Daniel Earwicker Avatar answered Oct 03 '22 00:10

Daniel Earwicker