Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

thread object

 
sai donthneni
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If an object implements 2 or more threads then will the member variables are shared between these 2 threads?
 
pascal betz
Ranch Hand
Posts: 547
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if the "variables" are acccessible from one thread, then why shouldnt they be visible to the other thread(s) ? threads are just java objects... same rules apply as with other objects.

if you have multiple threads accessing the same memeber variables, then you need to think about synchronizing access to these members and make sure you really need more than one thread.


p
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object doesn't so much "implement" multiple threads as it winds up being called by multiple threads. When one object has methods called by two threads, the member variables on that object can be a real problem unless you manage them with synchronization. A class with thorough protection is called "thread safe". Otherwise it is "unsafe" for multi-threading applications.

Try the Sun Thread Tutorial or scroll up to the Threads etc. forum if you want to dig into the details!
[ April 02, 2006: Message edited by: Stan James ]
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a diagram which depicts what Stan and Pascal stated, look at Q43

at free sample download.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic