Rajesh Kumar
Rajesh Kumar

Reputation: 89

How using a local object reference is thread safe?

I read a tutorial by jakob jenkov states,

public void someMethod(){

  LocalObject localObject = new LocalObject();

  localObject.callMethod();
  method2(localObject);
}

public void method2(LocalObject localObject){
  localObject.setValue("value");
}

to be thread safe. But I think

public class LocalObject extends Thread {
    public void run(){
        //LocalObject a= new LocalObject();
        method2(this);
    }

    public void someMethod(){
        LocalObject localObject = new LocalObject();

        localObject.callMethod();
        method2(localObject);
    }

    public void method2(LocalObject localObject){
        localObject.setValue("value");
    }
    public void setValue(String s){

    }
    public void callMethod(){

    }
    public static void main(String args[]){
        LocalObject a= new LocalObject();
        a.start();
        a.method2(a);
    }

}

is not thread thread safe as two thread 1. main & 2. LocalObject's thread is accessing the method2, which has a method setValue
Where have I misunderstood it?

Upvotes: 0

Views: 2369

Answers (2)

Hot Licks
Hot Licks

Reputation: 47729

First off, any time you create an object and store the object reference ONLY in an "automatic" variable (a variable declared within a method), and where you never later store that reference value into some other non-automatic location, that object can never "escape" from the thread where it was created. It does not matter if you pass the reference as a parameter to other methods (that observe the above restriction), since they are necessarily in the same thread.

And any object which is only referenced from a single thread is inherently "thread safe".

Similarly, if you have an instance of a subclass of Thread, and values (including possible references to other objects) are stored in that instance, this is "thread safe" so long as no other thread (including the initiating thread) is allowed to reference or modify the instance while the instance's thread is executing. Other possible instances of the same subclass will be referencing their own instances, not the first one mentioned -- they each have their own private "playpen".

Thread safety only becomes an issue when two different threads can access the same object instance, not simply one by the same name or of the same class.

Upvotes: 0

ppeterka
ppeterka

Reputation: 20726

The misunderstanding stems from the fact, that while there are two threads calling mathod2() at the same time, they are calling t on different instances of the class LocalObject:

public void run(){
    LocalObject a= new LocalObject(); //this is a new instance 
    method2(a);
}


public static void main(String args[]){
    LocalObject a= new LocalObject(); //this is an antirelz different instance
    a.start(); 
    a.method2(a);
}

If it was like

public void run(){
    //YIKES - not thread safe -- method 2 can be called from outside!
    method2(this); 
}

Then there would be an issue... But this would not pertain to the example in anz ways.

Upvotes: 7

Related Questions