Using variable or using direct method call

Discussion in 'Java' started by -, May 31, 2005.

  1. -

    - Guest

    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?

    Or should I just leave it that way rather than change it to
    getParent().callMethod(...) everytime...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?
     
    -, May 31, 2005
    #1
    1. Advertising

  2. -

    P.Hill Guest

    - 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
     
    P.Hill, May 31, 2005
    #2
    1. Advertising

  3. -

    Johan Poppe Guest

    - wrote:

    >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().callMethod(...) everytime...


    Depends on what you want and what you consider "fail".

    Basically, you are talking about a situation where the locally stored
    'parent' and the result of getParent() is no longer the same. That
    means that
    parent.callAnotherMethod()
    and
    getParent().callAnotherMethod()
    will call on different objects. The first variant will call on the
    container that was the parent when the method started. The second
    variant will call on the container that is the parent right now. I
    can't tell you which of the objects you _want_ to call on.

    >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?


    If you want to call on the same container object throughout your
    method, _and_ you want that object to be the actual parent-container,
    then you should synchronize on something.

    --
    Johan Utne Poppe
     
    Johan Poppe, May 31, 2005
    #3
  4. P.Hill coughed up:
    > - 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.



    What are you talking about? The parent object was returned by a getParent()
    method and could possibly be publicly accessible. That object is able to be
    mutated at any time! No one is worried about the "parent" variable itself.


    ....[rip]...


    --
    Onedoctortoanother:"Ifthisismyrectalthermometer,wherethehell'smypen???"
     
    Thomas G. Marshall, Jun 1, 2005
    #4
  5. -

    P.Hill Guest

    Thomas G. Marshall wrote:
    > No one is worried about the "parent" variable itself.


    The OP did not make that clear, so I thought to mention it.

    As Johan Poppe wrote:
    > Depends on what you want and what you consider "fail".


    -Paul
     
    P.Hill, Jun 1, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Thomas_Gagn=E9?=
    Replies:
    9
    Views:
    11,294
    Jezuch
    Jul 3, 2003
  2. mcdeveloper
    Replies:
    1
    Views:
    4,280
    mcdeveloper
    Jun 13, 2006
  3. Amy Matlock
    Replies:
    4
    Views:
    364
    Amy Matlock
    Nov 10, 2005
  4. Bryan Richardson

    Using string variable to call a method

    Bryan Richardson, Jan 7, 2008, in forum: Ruby
    Replies:
    6
    Views:
    131
    Bryan Richardson
    Jan 8, 2008
  5. F. Da Costa
    Replies:
    0
    Views:
    170
    F. Da Costa
    Feb 2, 2004
Loading...

Share This Page