• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference between light/heavy weight threads

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to understand the difference between the light/heavy weight threads. i went through google,it does gives me answers but i completely did not get that. When they say "same address space",what do they actually mean??? .so i thought i will as this in my way(No sure if it is the right way to ask. )
Suppose i have this program.
Here there are two threads, when the first thread is in sleep, the second thread is invoked.Now as a result of switching between threads, is it light weight or heavy weight???
Also could you explain how did you say it is light/heavy weight. Also please tell in terms of address space.
And if this example is not correct to discuss about light/heavy weight thread,please provide a correct program.

 
Maneesh Godbole
Saloon Keeper
Posts: 11165
14
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We have a dedicated forum for thread related questions. I will move this there for you.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe it's just me, but I think many Java programmers will not know what you/re talking about here. I don't. I have not seen any standardized usage of "heavyweight" and "light" when talking about threads in Java. I suppose I might guess at what you might mean, but I think it would be better if you could tell us. Where have you seen this usage? Are you using a particular operating system? A particular JVM? (Um, you are using Java, right?) Is it possible you read something about green threads? Or maybe something about lightweight and heavyweight Swing components? So far, I'm lost here.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually i want want to seen a program in which there are heavy weight threads and a program where there are light weight threads.
So that i would have knowledge to clearly identify/distinguish and say , yes these are heavy weight threads and those are light weight threads.
Hope i am clear now.
I am using java and i have not heard of green thread
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.

So, where have you heard about "heavyweight threads"? Or "lightweight threads"? Who else talks about these things? Why does it matter?
 
Adam Michalik
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I never heard the terms either, but found this. Seems like it's an OS-dependant thing, nothing to do with Java. Java threads are backed by OS threads, so if your OS's threads are heavyweight, Java ones will be, too. The same would apply to middle- and lightweight ones. But it's only as much as I can say having read this short article.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.. all this started when i was trying to find the difference between CGI and jsp/servlets .
For every request to a CGI it will create a heavy weight thread. But in JSP/servlet a a light weight thread is created. This is what the cause of my doubt.
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Tharakan wrote:Ok.. all this started when i was trying to find the difference between CGI and jsp/servlets .
For every request to a CGI it will create a heavy weight thread. But in JSP/servlet a a light weight thread is created. This is what the cause of my doubt.


A 'Heavyweight Thread' creates a new memory address for the thread to run in - completely isolating it from other 'threads'. We usually call these 'Processes'. In Java all Threads are lightweight, they share the same Processes and memory space so data is easily shared between them by shifting into and out of a common memory area into thread-local areas. To use a 'Heavyweight' thread in Java you would create a new Process, which is akin to running a new application, for which you use Runtime.exec() or the ProcessBuilder. In these cases a new Process is made and communication between the two Processes is not easy - on the other hand each Process can run without worry of one process corrupting the data in the other process, no need to synchronize data access.
 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay .. now things are clear,when you said how to start a new heavy weight process...
Thanks
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic