Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heavy weight and light weight thread

What are the Light weight and heavy weight threads in terms of Java?

like image 685
rocker Avatar asked Feb 10 '10 11:02

rocker


People also ask

What is heavy thread and lightweight thread?

Threads are typically compared in terms of processing time. For example, a lightweight thread is a thread that takes less processing time, whereas a heavyweight thread is a thread that requires more processing time. Thread processing time is also contingent on the language used for thread implementation.

Why process is heavy weight and thread is light weight?

Threads are sometimes called lightweight processes because they have their own stack but can access shared data. Because threads share the same address space as the process and other threads within the process, the operational cost of communication between the threads is low, which is an advantage.

What is heavy weight thread?

Heavy weight threads range from 12wt – 3wt. 12wt threads can still fit through the eye of a sewing machine needle, so you can do some incredible stitching with this thicker thread. Whereas 8wt and heavier threads are not recommended as a top thread.

What makes a thread lightweight?

Also threads within a process share the same address space because of which cost of communication between threads is low as it is using the same code section, data section and OS resources, so these all features of thread makes it a "lightweight process".


1 Answers

It's related to the amount of "context" associated with a thread, and consequently the amount of time it takes to perform a "context switch".

Heavyweight threads, (usually kernel/os level threads) have a lot of context (hardware registers, kernel stacks, etc). So it takes a lot of time to switch between threads. Heavyweight threads may also have restrictions on them, for example, on some OSes, kernel threads cannot be pre-empted, which means they can't forcibly be switched out until they give up control.

Lightweight threads on the other hand (usually, user space threads) have much less context. (They essentially share the same hardware context), they only need to store the context of the user stack, hence the time taking to switch lightweight threads is much shorter.

On most OSes, any threads you create as a programmer in user space will be lightweight in comparison to the kernel space threads. There is no formal definition of heavyweight and lightweight, it's just more of a comparison between threads with more context and threads with less context. Don't forget that every OS has its own different implementation of threads, and the lines between heavy and light threads are not necessarily clearly defined. In some programming languages and frameworks, when you create a "Thread" you might not even be getting a full thread, you might just be getting some abstraction that hides the real number of threads underneath.

[Some OSes allow threads to share address space, so threads that would usually be quite heavy, are slightly lighter]

like image 123
Simon P Stevens Avatar answered Oct 14 '22 07:10

Simon P Stevens