aspose file tools*
The moose likes Threads and Synchronization and the fly likes instance members, shared? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "instance members, shared?" Watch "instance members, shared?" New topic
Author

instance members, shared?

Colin Chow
Greenhorn

Joined: Jan 23, 2001
Posts: 12
The main difference between multithreading model and the process model is that among threads, it has shared data to all the threads and local data to each individual. In the process model, every data is local to each process. Now the question on multithreadin....
I was pretty clear, at least I thought I was, on the concept that if threads inherits (or implements) from the same class/interface, the instance variables, even though they are declared private, of such threads are shared (meaning have only one copy). The only local data of a thread after it's started is the local variables inside the run() method. I learned this concept after reading the Java Threads book from OReily and the red Java Server Programming book from Wrox. Even quoted them one time in an interview debate. So I am sure what I read.
Then today, I was doing some testing. I created one inner class that implements Runnable (extends Thread, either way) and have a few private, object instance variables in it. Having spinned off two threads, I made some modification (synchronized access/modify methods) to each thread and get the values of the instance variables. The result I expected was both thread1 and thread2's Vector type instance variable would have the same element when I put the object only in thread1's vector. It turned out it's not true. thread1 has the object I put in; thread2 doesn't.
Now I am confused, because this contradicts what I learned from 2 pretty well known java books and respected authors. The ultmiate question is: is the private instance variables of the threads inherits from the same class/interface shared ie. modifiable by other threads, regardless of their private-ness? Or are these private instance variables local to each thread? (sorry the wordiness, but just want to make it clear.)
Thanks for the input!
Colin
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Colin,
Sorry to ask but it's not clear from your message: Are you sure that you have only one instance of the inner-class that you created? If your inner-class extends Thread then obviously you'd be creating 2 instances, one for each thread that you start.
Colin Chow
Greenhorn

Joined: Jan 23, 2001
Posts: 12
I actually created 2 instances of the inner class. ie, I have 2 threads running, and these 2 threads are inherited from the same class, Thread. So according to the books I read, the private instance variables in these 2 threads should be shared. That is, the instance variables declared in this inner class should only one copy. Furthermore, if I modify the thread1's instance variable, when I use the access method on thread2 to return the value, this should return the value of what I modified in thread1. However, from the test I ran, this is not true. When I modified thread1's private instance variable via the method, the change does not reflect on thread2's--meaning--even though they have instance variables, these variables are local to each thread instance.
sorry about the length (ignore the increativity of the class name. The output from below class is:
C:\development>java testpi
t2 0
t1 null
t2 I am a string
thread t2: I am a string
thread t2: 0
thread t2: null
thread t1: I am string one
thread t1: 999
thread t1: testpi$someObject@6930e2
t1 testpi$someObject@6930e2
t2 testpi$someObject@8786b
false
thread t2: I am a string
thread t2: 0
thread t2: testpi$someObject@8786b
thread t1: I am string one
thread t1: 999
thread t1: testpi$someObject@6930e2
thread t2: I am a string
thread t2: 0
thread t2: testpi$someObject@8786b
thread t1: I am string one
thread t1: 999
thread t1: testpi$someObject@6930e2

public class testpi
{
public testpi()
{
testThread t1 = new testThread("t1");
t1.start();
testThread t2 = new testThread("t2");
t2.start();
t1.setValue(999);
System.out.println("t2 "+t2.getValue());
t2.setString("I am a string");
System.out.println("t1 "+t1.getString());
t1.setString("I am string one");
System.out.println("t2 "+t2.getString());
t1.setObject();
System.out.println("t1 "+t1.getObject());
t2.setObject();
System.out.println("t2 "+t2.getObject());
System.out.println(""+(t1.getObject()==t2.getObject()));
try {new Thread().sleep(10L);} catch (Exception e) {}
}
public static void main(String arg[])
{ testpi test = new testpi(); }

class testThread extends Thread
{
private int value;
private String string;
private boolean stop;
private String name;
private someObject so;

public testThread(String name) {this.name = name;}

public synchronized void setValue(int number)
{ this.value = number; }

public synchronized void setString(String message)
{ this.string = message; }

public synchronized void setObject()
{ so = new someObject(); }

public synchronized int getValue()
{ return value; }

public synchronized String getString()
{ return string; }

public synchronized someObject getObject()
{ return so; }

public synchronized void stoppy()
{ stop = true; }

public void run()
{
String td = new String("thread "+name+": ");
while(!stop)
{
System.out.println(td+getString());
System.out.println(td+getValue());
System.out.println(td+getObject());
try {Thread.sleep(5000L);} catch (InterruptedException e) {}
}
System.out.println(td+"quit");
}
}

class someObject
{
private java.util.Vector v;
private java.text.SimpleDateFormat smf;

public someObject()
{
v = new java.util.Vector();
smf = new java.text.SimpleDateFormat();
v.addElement(new Object());
}
}
}
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Colon,
My understanding differs from yours on the private instance members. Instance members are not shared between different instances of the same class. It doesn't matter whether they are public, private, or protected. The only way to make two instances of the same class share the same member is to make that member static. In this case all instances of the class refer to the same static member.
Colin Chow
Greenhorn

Joined: Jan 23, 2001
Posts: 12
Yes, I understand that concept and very agreed. The reason I am struggle is that the books (Java Threads from OReily and others) I read say repeatedly in threading, if one class implements or inherits from the same class or interface, and become a thread, the instance variables are shared, ie one copy. Then they go on and say that the only local data to one thread is the local variables declared inside the run() method.
That's why I am confused and want to seek out what the authors meant. I can post the quote from the books when I get ahold of them later (at home).

Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Colin,
I'd be glad to look at the text from the book. In the meantime, I can guess. Suppose that the instance variables that the text talks about are defined in the outer-class rather than the inner-class. In that case, I agree the threads would be sharing the same instance.
Colin Chow
Greenhorn

Joined: Jan 23, 2001
Posts: 12
Mark, thanks for the help!
Well I don't think the inner class would have anything to do with it though I just quickly ran the test again with the inner classes separate to be 2 public classes by themselves (public class testThread and public class someObject).
The output I got is the same! The memory reference of someObject (instance memeber) is different. ie, they are different copies.
C:\development>java testpi
t2 0
t1 null
t2 I am a string
thread t2: I am a string
thread t2: 0
thread t2: null
thread t1: I am string one
thread t1: 999
thread t1: someObject@6930e2
t1 someObject@6930e2
t2 someObject@8786b
false
thread t2: I am a string
thread t2: 0
thread t2: someObject@8786b
thread t1: I am string one
thread t1: 999
thread t1: someObject@6930e2
Again, I wish I had the book handy and perhaps I can find some way to contact the authors from it.
Mark, thanks again!
Mark Savory
Ranch Hand

Joined: Feb 08, 2001
Posts: 122
Colin,
Not to beat a dead cat but just one last. Normally, a thread is created in order to make a block of code in an existing class asynchronous. As per the examples that I follow in practice, this is done by implementing the Runnable interface in an anoymous class(or inner-class) and puttting the asynchronous block of code in the run() method. Then you start the thread within a particular method of the existing class - like main. There's no reason that I can think of to create an attribute in the anoymous class. You want to start the thread and have it operate on the attributes of the existing class, which will be shared for all threads started within the main method, for example.
Colin Chow
Greenhorn

Joined: Jan 23, 2001
Posts: 12
Mark!
You are dead on! I now remember the context of what the books talk about. Ha! You just solved my problem. Thank you! Thanks for straightening things out for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: instance members, shared?