Re: Using variable or using direct method call
- From: "P.Hill" <goodhillREMOVE@xxxxxxxxxxxxxxxxxx>
- Date: Mon, 30 May 2005 22:30:55 -0700
- 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 .
- References:
- Prev by Date: Open Source Document Management in Java
- Next by Date: List of Platform which J2ME MIDP supported
- Previous by thread: Using variable or using direct method call
- Next by thread: Re: Using variable or using direct method call
- Index(es):
Relevant Pages
|