Guards iteration against concurrent modification 64/65464/2
authorJie Han <han.jie@zte.com.cn>
Mon, 13 Nov 2017 03:26:39 +0000 (11:26 +0800)
committerJie Han <han.jie@zte.com.cn>
Mon, 13 Nov 2017 03:36:43 +0000 (11:36 +0800)
- fix or it would throw a ConcurrentModificationException.
Change-Id: I39664b2238ef62d2add96cb76ac8c2113cfc2265
Signed-off-by: Jie Han <han.jie@zte.com.cn>
opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/sal/dom/broker/BrokerImpl.java

index 33fc244..7998bde 100644 (file)
@@ -79,9 +79,11 @@ public class BrokerImpl implements Broker, DOMRpcProviderService, DOMRpcService,
     // Validation
     private void checkPredicates(final Provider prov) {
         Preconditions.checkNotNull(prov, "Provider should not be null.");
-        for (final ProviderContextImpl session : providerSessions) {
-            if (prov.equals(session.getProvider())) {
-                throw new IllegalStateException("Provider already registered");
+        synchronized (providerSessions) {
+            for (final ProviderContextImpl session : providerSessions) {
+                if (prov.equals(session.getProvider())) {
+                    throw new IllegalStateException("Provider already registered");
+                }
             }
         }
 
@@ -89,9 +91,11 @@ public class BrokerImpl implements Broker, DOMRpcProviderService, DOMRpcService,
 
     private void checkPredicates(final Consumer cons) {
         Preconditions.checkNotNull(cons, "Consumer should not be null.");
-        for (final ConsumerContextImpl session : sessions) {
-            if (cons.equals(session.getConsumer())) {
-                throw new IllegalStateException("Consumer already registered");
+        synchronized (sessions) {
+            for (final ConsumerContextImpl session : sessions) {
+                if (cons.equals(session.getConsumer())) {
+                    throw new IllegalStateException("Consumer already registered");
+                }
             }
         }
     }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.