*
The moose likes Beginning Java and the fly likes thread object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "thread object" Watch "thread object" New topic
Author

thread object

sai donthneni
Ranch Hand

Joined: Mar 26, 2006
Posts: 50
If an object implements 2 or more threads then will the member variables are shared between these 2 threads?
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
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

Joined: Jan 29, 2003
Posts: 8791
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 ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
ak pillai
author
Ranch Hand

Joined: Feb 11, 2006
Posts: 288
For a diagram which depicts what Stan and Pascal stated, look at Q43

at free sample download.


java j2ee job interview questions with answers | Learn the core concepts and the key areas
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: thread object