This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Threads and Synchronization and the fly likes Difference between light/heavy weight threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Difference between light/heavy weight threads" Watch "Difference between light/heavy weight threads" New topic
Author

Difference between light/heavy weight threads

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

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.



SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10167
    
    8

We have a dedicated forum for thread related questions. I will move this there for you.


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2982
    
    9
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

Joined: Aug 29, 2008
Posts: 580

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

Joined: Mar 05, 2008
Posts: 2982
    
    9
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

Joined: Feb 18, 2008
Posts: 128
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

Joined: Aug 29, 2008
Posts: 580

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

Joined: Jan 28, 2003
Posts: 4161
    
  21

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.


Steve
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Okay .. now things are clear,when you said how to start a new heavy weight process...
Thanks
 
Don't get me started about those stupid light bulbs.
 
subject: Difference between light/heavy weight threads
 
Similar Threads
Thread question
Thread Behaviour? Help!!!
Will a started thread run to completion in this example?
Threading synchronization
join() method in Thread doesn't work as expected