Results 1 to 3 of 3
  1. #1
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default Preventing local copies of fields

    A little background. I want to dispose of certain instance properties, when I am done with the object, that I would normally declare final. I want to dispose of them because they create circular references. (I know, that should be avoided, but anything resembling a listener does that.) Most important, I want to avoid the possibility of threads making local copies of these properties. Normally, declaring them final does that.

    My question is this: If I create a simple container object and declare that final, and then I put the actual property inside the container, will that prevent threads from making local copies? Looking at it, I think I will still have to declare t as volatile...

    Java Code:
    class SimpleContainer<T extends Object> {
      private T t;
    
      SimpleContainer(final T tParm) {
        t = tParm;
      }
    
      T get() {
        return t;
      }
    
      void clear() {
        t = null;
      }
    }

  2. #2
    vogella is offline Member
    Join Date
    Aug 2009
    Location
    Heidelberg
    Posts
    47
    Rep Power
    0

    Default

    I think it is hard to avoid that another thread creates a copy of an object. Is that really what you want or is it more about preventing a thread of changing the same object? You could do this via a defensive copy in the getter so that you never return the actual object but only a copy. Making the object immutable of couse also helps to avoid any changes by other threads.

  3. #3
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Posts
    1,046
    Rep Power
    8

    Default

    Sort of both. I decided the overhead did not warrant the effort, even if it would work. I like to final fields in general, just to make clear that other programmers should not alter it, and set a field final in threaded applications avoids copy issues. On the other hand, copy issues are not significant if the value is set once at the outset and then only changed to null references, and a comment works (almost) as well as final.

Similar Threads

  1. PS Prinitng Issue - Copies not working
    By nvngyl in forum Advanced Java
    Replies: 1
    Last Post: 08-12-2009, 08:30 PM
  2. preventing access/locking a folder?
    By solris in forum New To Java
    Replies: 1
    Last Post: 06-29-2009, 01:20 AM
  3. Keeping track of Copies + Comparisons in Sort Methods
    By VinceGuad in forum New To Java
    Replies: 0
    Last Post: 03-12-2009, 05:07 AM
  4. Jsp / Java cannot get fields
    By Dracos in forum New To Java
    Replies: 0
    Last Post: 02-04-2008, 10:39 PM
  5. Replies: 2
    Last Post: 08-10-2007, 01:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •