| Author |
synchronised add and remove listener methods
|
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
Hi, My IDE (NetBeans) creates this source (below) for the "multicast event source pattern". My question is: why are the add and remove listener methods synchronised ? Is it something to do with the fact that the listenerList is lazily instantiated ? thanks, Tom
|
 |
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
|
|
Originally posted by Don Kiddick: [...] Why are the add and remove listener methods synchronised ? Is it something to do with the fact that the listenerList is lazily instantiated?
Yes. If you don't use lazy instantiation, there is no need for the synchronization (see the javadoc for EventListenerList). However, I would contend that the code you presented is not guaranteed to be threadsafe. Because fireTabbedViewerListenerPictureDisplayed() is not synchronized, the JLS offers no guarantee that a listenerList instantiated in addTabbedViewerListener() will actually be visible to another thread calling fireTabbedViewerListenerPictureDisplayed(). Also, the correct behaviour of removeTabbedViewerListener() depends on addTabbedViewerListener() having been called beforehand, which could give problems in some cases. Whoever thought this was a good idea broke theFirst Rule of Optimisation. - Peter [ January 14, 2003: Message edited by: Peter den Haan ]
|
 |
Don Kiddick
Ranch Hand
Joined: Dec 12, 2002
Posts: 580
|
|
Cheers Peter ! T.
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1158
|
|
|
I checked the docs and saw no indication that the EventListenerList was thread safe. as a result the add and remove should be synchronized in a multithreaded environment. Along with at least part of the firing method.
|
 |
Peter den Haan
author
Ranch Hand
Joined: Apr 20, 2000
Posts: 3252
|
|
Originally posted by CL Gilbert: I checked the docs and saw no indication that the EventListenerList was thread safe.
"The main benefits that this class provides are that it is relatively cheap in the case of no listeners, and it provides serialization for event-listener lists in a single place, as well as a degree of MT safety (when used correctly)." -- introduction to the EventListenerList javadoc (my emphasis). Note in this respect the absence of explicit synchronization in the sample code that illustrates correct usage. Hope this helps, - Peter [ February 03, 2003: Message edited by: Peter den Haan ]
|
 |
Mr. C Lamont Gilbert
Ranch Hand
Joined: Oct 05, 2001
Posts: 1158
|
|
Originally posted by Peter den Haan: "The main benefits that this class provides are that it is relatively cheap in the case of no listeners, and it provides serialization for event-listener lists in a single place, as well as a degree of MT safety (when used correctly)."
I don't know what to say about such a vague statemet. Either its thread safe, or its not. Their is no such thing as 1/2 thread safe. So this whole statement is misleading. If its thread safe as you indicate they should simply call it thread safe, and let go of the hemming and hawing. Obviously if you introduce your own errors you can unthreadsafetify any code.
|
 |
 |
|
|
subject: synchronised add and remove listener methods
|
|
|