Re: Using variable or using direct method call



- wrote:
If I were to do this:

public void method() {
    Container parent = getParent();
[...]

parent.callMethod(...);

[...]

parent.callAnotherMethod(...);

}


and if another thread were to change the parent, will the above fail since the method is not synchronized?

Another thread can't change the LOCAL variable which references parent, because locals are on the stack of each thread.


Now, if "Container parent" was a member, then we'd be talking about
a problem.

On the other hand, there could be thread problems if:
1. two threads called callMethod() both of which change the
members of parent.

2. callMethod() and callAnotherMethod() were expecting the state of the members of parent to be stable between these two calls,

In either case you have a problem.

One solution for (1) is to synchronize callMethod() and callAnotherMethod().

One solution for (2) might be

synchronized ( parent ) {
    parent.callMethod(...);
[...]
    parent.callAnotherMethod(...);
}

But if there is anything to do between the calls which involves
any other synchronized objects you have deadlock potential.

-Paul
.



Relevant Pages

  • Using variable or using direct method call
    ... public void method() { ... and if another thread were to change the parent, will the above fail since the method is not synchronized? ... Or should I just leave it that way rather than change it to getParent.callMethodeverytime...but doing so is of no use since if the parent changes midway, the method's objective will not be met since it requires that the parent be the same throughout the method's scope? ...
    (comp.lang.java.programmer)
  • Re: hide members in parent-child
    ... members for parent nodes are visible, hidden, or not allowed. ... - Child 1 ... The second option that I can think of is the "Skipped Levels Column" ...
    (microsoft.public.sqlserver.olap)
  • Re: Named Set Alias
    ... > see the "parent" calculation but this will not let you drill down. ... > 2) Introduce duplicate members into the dimension to create a fake ... > the secondary hierarchy from affecting the "All member" totals. ... >> I have a named set with 5 non-leaf members from the same dimension, ...
    (microsoft.public.sqlserver.olap)
  • Inner Classes Change Access Rights Of Parent Members?!?
    ... I read that if you access a parent class's private memebers or methods ... from within an inner class, ... need access to certain private members and methods of their parent. ...
    (comp.lang.java.programmer)
  • Re: hide members in parent-child
    ... I have an unbalanced hierarchy, but not ragged, on which I needed to hide ... some leaf members according to a rule. ... Members With Data doesn't solve it, ... >> members for parent nodes are visible, hidden, or not allowed. ...
    (microsoft.public.sqlserver.olap)