Nishi
Nishi

Reputation: 125

Local object are used as method parameter then that objects are not thread safe. why?

The only exception is of course, if one of the methods called with the LocalObject as parameter, stores the LocalObject instance in a way that allows access to it from other threads. Please calrify me on this. how its work.

public void someMethod(){

  LocalObject localObject = new LocalObject();

  localObject.callMethod();

// localObject is not threadsafe
  method2(localObject);
}

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

Upvotes: 0

Views: 1927

Answers (4)

Andreas Dolk
Andreas Dolk

Reputation: 114837

Here is an example how to make it not threadsafe:

private List<LocalObject> list = new ArrayList<LocalObject>();
public List<LocalObject> getList(){return list};

public void someMethod(){

  LocalObject localObject = new LocalObject();

  localObject.callMethod();

// localObject is not threadsafe
  method2(localObject);
}

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

method2 was modified to store the localObject in a way that allows other threads to access it - to a public accessible list. Another thread now could get the object from the list and set the value at the very same time. So even if we work with a local object, it is not threadsafe anymore and we have to think about synchronization.

Upvotes: 1

djna
djna

Reputation: 55957

You seem to be referring to this article, which has almost exactly the code snippet you present. However, this article claims that the code you show is thread safe.

So, it seems that you have some reason to assert that this code is not thread safe, but haven't told us the reason for this assertion.

For the code you show the only threading vulnerability is if LocalObject itself has some threading issues itself, for example if it had static data items that were not protected correctly.

I think perhaps you are asking for this kind of example

 public class UnsafeClass{
    static LocalObject bestWeHaveSeen = new LocalObject();

    public void someMethod(){

        LocalObject localObject = new LocalObject();

        localObject.callMethod();

        // localObject is not threadsafe
        method2(localObject);
    }

 public void method2(LocalObject localObject){
    if ( localObject.getValue("value") > bestWeHaveSeen.getValue("value") ){
         bestWeHaveSeen = localObject;
    }

    // some code here to compute an important value
    bestWeHaveSeen.setValue(computedValue);
 }

This is now not thread safe. As two different threads can access bestWeHaveSeen

Upvotes: 3

Victor Hurdugaci
Victor Hurdugaci

Reputation: 28435

A scenario is not thread safe (Tn = thread n):

T1 calls someMethod first; T2 calls someMethod after that.

When executing method2 on T1, the reference to localObject is changed by T2 leading to some undefined behavior.

Upvotes: 0

seand
seand

Reputation: 5296

I don't see an obvious MT safety problem with this. 'localObject' doesn't appear to be leaking anywhere enabling other threads to see it.

Upvotes: 0

Related Questions