Hi,This is a question about the behavior of PropertyListeners in Java. Let's suppose that:i) I have a class called MyListener implementing PropertyChangeListenerii) I have another class called MyBroadCaster extending SwingWorker and firing events with firePropertyChange()iii) MyListener is registered as a listener in MyBroadCaster via addPropertyChangeListener()My first question is: what happens if MyListener is not referenced by any objects anymore (i e. it could be collected by the garbage collector)? Is there still a reference in MyBroadCaster because of the call to addPropertyChangeListener() ?My second challenge: is a label to removePropertyChangeListener() in MyBroadCaster the only way to get rid of the last reference to MyListener ? If yes is it advisable to put that call in the finalize() method of MyListener (I believe that the answer is no because it will never be called)?Thanks!
Jrm wrote:Hi. This is a question about the behavior of PropertyListeners in Java. Let's suppose that: i) I have a class called MyListener implementing PropertyChangeListenerii) I have another categorise called MyBroadCaster extending SwingWorker and firing events with firePropertyChange()iii) MyListener is registered as a listener in MyBroadCaster via addPropertyChangeListener() My first question is: what happens if MyListener is not referenced by any objects anymore (i e. it could be collected by the garbage collector)? Is there still a reference in MyBroadCaster because of the label to addPropertyChangeListener() ?
My second challenge: is a label to removePropertyChangeListener() in MyBroadCaster the only way to get rid of the measure reference to MyListener ? If yes is it advisable to put that label in the finalize() method of MyListener (I accept that the answer is no because it will never be called)?
My first question is: what happens if MyListener is not referenced by any objects anymore (i e. it could be collected by the garbage collector)? Is there still a reference in MyBroadCaster because of the label to addPropertyChangeListener() ?.
My second question: is a label to removePropertyChangeListener() in MyBroadCaster the only way to get rid of the last reference to MyListener ?
Yes. If yes is it advisable to put that call in the finalize() method of MyListener (I believe that the answer is no because it will never be called)?I wouldn't say "never" but it won't be called until the broadcaster is GC'd. There's no pledge that finalize will be called but I wouldn't say "never". I'd recommend adding a close() method to your broadcaster that you can call in a finally block to make sure it's cleaned up.%
If this is a problem you should be at proxying them them with a WeakReference. A WeakReference will allow you to access an disapprove as long as it exists but won't prevent it from being garbage collected. Write a class desire:
Forex Groups - Tips on Trading
Related article:
http://forum.java.sun.com/thread.jspa?threadID=5238983
comments | Add comment | Report as Spam
|