Reputation: 7728
I have been reading about Java multi threading. I came up across a topic on the resources share across threads here. It got me a bit confused about the local object references.
Local references to objects are a bit different. The reference itself is not shared. The object referenced however, is not stored in each threads's local stack. All objects are stored in the shared heap. If an object created locally never escapes the method it was created in, it is thread safe. In fact you can also pass it on to other methods and objects as long as none of these methods or objects make the passed object available to other threads.
The first couple of lines I can understand, however what does the author mean with the line in bold above ? More specifically, what does he mean by locally escaping ?
Could some one please care to explain, possibly with a different example.
Upvotes: 5
Views: 3476
Reputation: 2288
First of all, all the local reference which you create inside a method, they are stored in the method local stack - hence they are thread safe. But the object which you create in any method will still be allocated in the shared heap space. But since your local variable is pointing to that object , no other variable has the access to it - unless you assign the handle to some other variable from other thread (or any other non - thread safe reference)
Please find below an attempt for explaination :
public class Test {
Map<String, String> stringMap = null;
public void method1() {
Map<String, String> method1Map = new HashMap<String, String>();
// method1Map.<some method>
// map is thread safe till now
method2(method1Map);
}
public void method2(Map<String, String> map) {
// map.<some method>
// map is still thread safe till now
// after the below line the map is no more thread safe as the instance
// variable got the handle to same map
this.stringMap = map;
}
}
Upvotes: 3
Reputation: 540
The method can be called by different threads. And there will be created a new instance for every call and for every thread. But no one of the created instances can be shared to another thread, the instance exists only in the local method stack.
public void someMethod(){
//First and second thread calls method, both get there own object
LocalObject localObject = new LocalObject();
localObject.callMethod();
method2(localObject);
}
//calling next method, own object, too. Instance of the method stack of thread one or two
public void method2(LocalObject localObject){
localObject.setValue("value");
}
Think about the opposite, an instance member of the class. Two threads can call the method
InstanceObject instObject;
public void someMethod(){
//First and second thread calls method, both initialize the member var
if(instanceObject == null)
instObject = new InstanceObject ();
instObject.callMethod();
method2(localObject);
}
public void method2(){
instObject.setValue("value");
}
Now that's not threadsafe. Thread one call someMethod and started to initialize instObject. Thread two comes a bit later. instObject is null for thread two, too. That can happen because of two reasons:
instObject is not completely initiliazed
the value of instObject is not passed from the memory of thread one to the memory of thread two
And when thread one changes instObj it expected the object to have a specific value. But than comes thread two and changes this value. Or -even worse- thread one and thread two changes the instObject, but the changes exist only in the memory of the particular threads.
Upvotes: 0
Reputation: 136002
In this test HashSet usage is thread safe because the only reference to it is in a local variable and if another thread enters this method it will create a new Set.
void x() {
Set s = new HashSet();
s.add(1);
}
but if we save reference to our HashSet in a field then another thread may change it concurrently
Set s;
void x() {
s = new HashSet();
s.add(1);
}
void y() {
s.add(2);
}
Upvotes: 3
Reputation: 2327
"Thread safe" means that there is no possibility that an object is put into an improper state or some other error is caused by two threads modifying the same data at once; see eg. What does threadsafe mean? and What is meant by thread-safe code?.
Locally created objects, that is objects that you create in a method and store in a local variable of that method, can normally only be accessed by code in that method. When the object is return
ed to the calling method or passed as a parameter to another method (including when you call methods of the object itself), those methods gain access to it, and the object is said to have escaped. Those methods might then publish it somewhere that other threads can see it and attempt to modify it, potentially causing thread-safety problems. If however an object never escapes, then it is guaranteed that no other threads will ever gain access to it, and therefore that your use of the object will always be thread-safe.
Upvotes: 1