mbien commented on code in PR #6872:
URL: https://github.com/apache/netbeans/pull/6872#discussion_r1443473341


##########
enterprise/javaee.wildfly/src/org/netbeans/modules/javaee/wildfly/ide/ui/AddServerLocationPanel.java:
##########
@@ -44,25 +42,15 @@ public class AddServerLocationPanel implements 
WizardDescriptor.FinishablePanel,
 
     private AddServerLocationVisualPanel component;
     private WizardDescriptor wizard;
-    private final transient Set listeners = ConcurrentHashMap.newKeySet(2);
+    private final transient Set<ChangeListener> listeners = 
ConcurrentHashMap.newKeySet(2);
 
     public AddServerLocationPanel(WildflyInstantiatingIterator 
instantiatingIterator) {
         this.instantiatingIterator = instantiatingIterator;
     }
 
     @Override
     public void stateChanged(ChangeEvent ev) {
-        fireChangeEvent(ev);
-    }
-
-    private void fireChangeEvent(ChangeEvent ev) {
-        Iterator it;
-        synchronized (listeners) {
-            it = new HashSet(listeners).iterator();
-        }
-        while (it.hasNext()) {
-            ((ChangeListener) it.next()).stateChanged(ev);
-        }
+        listeners.iterator().forEachRemaining(l -> l.stateChanged(ev));

Review Comment:
   you sure this is safe? I think what the original code attempted to do is to 
shapshot a copy of the listeners, and then iterate over that copy, probably 
anticipating that the listeners set can change at any point in time.
   
   The iterator won't protect from concurrent access, the javadoc of CHM says 
"However, iterators are designed to be used by only one thread at a time".
   
   alternative proposal in the spirit of the original:
   ```java
           List<ChangeListener> copy;
           synchronized (listeners) {
               copy = new ArrayList<>(listeners);
           }
           copy.forEach(l -> l.stateChanged(ev));
   ```
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to