wood burning stoves*
The moose likes Threads and Synchronization and the fly likes Threads and light weight Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Threads and light weight" Watch "Threads and light weight" New topic
Author

Threads and light weight

Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
Hi,
In one of the books i read that "Threads are light weight components, because they share data and code"
I feel that sharing of data will have data corruption between the threads. Am i missing something here ?
Can some one throw light on this ?
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Threads are light-weight *processes* - "components" seems to be a little bit misleading to me...
Certainly sharing data might lead to data corruption if done improperly, so you really need to know what you are doing. OTOH, proper synchronization between threads isn't *that* hard to accomplish, once you understood the basics of concurrent programming...


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Hari babu
Ranch Hand

Joined: Jun 25, 2001
Posts: 208
Hi,
When it says that "thread share code", what and how does that happen.
Please help
Hari
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 985

Do you mean 'thread safe code'?
Multiple threads can use any data objects which are available and were created before the threads started running. This can cause problems when more than one thread tries to use the same data object. Making something 'thread safe' guards against this problem. Some predefined java items are 'thread safe' by default. Others need you to wrap them.
Chapter 14 of this free (for download) book talks about threads and making things 'thread safe'.
Thinking in Java


Please ignore post, I have no idea what I am talking about.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Hari babu:
When it says that "thread share code", what and how does that happen.

If two threads execute the same method, they are accessing the same byte code (or compiled code respectively) at the same memory location.
When using heavy wheight processes (in Java started by Runtime.exec, for example), they would have totally seperate memory - even speperate JVMs.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Threads and light weight