• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads and light weight

 
Hari babu
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Hari babu
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
When it says that "thread share code", what and how does that happen.
Please help
Hari
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic