This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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 ?
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
Joined: Jun 25, 2001
Hi, When it says that "thread share code", what and how does that happen. Please help Hari
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.
Joined: Jul 11, 2001
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.