• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Threads and light weight

 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
When it says that "thread share code", what and how does that happen.
Please help
Hari
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic