/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloudera.llama.am.impl;

import com.cloudera.llama.am.api.LlamaAM;
import com.cloudera.llama.am.api.LlamaAMEvent;
import com.cloudera.llama.am.api.LlamaAMListener;
import org.apache.hadoop.conf.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashSet;
import java.util.Set;

public abstract class LlamaAMImpl extends LlamaAM {
  private static final Logger LOG = LoggerFactory.getLogger(LlamaAMImpl.class);
  private final Set<LlamaAMListener> listeners;

  protected LlamaAMImpl(Configuration conf) {
    super(conf);
    listeners = new HashSet<LlamaAMListener>();
  }

  public void addListener(LlamaAMListener listener) {
    synchronized (listeners) {
      listeners.add(listener);
    }
  }

  public void removeListener(LlamaAMListener listener) {
    synchronized (listeners) {
      listeners.remove(listener);
    }
  }

  protected void dispatch(LlamaAMEvent event) {
    if (!event.isEmpty()) {
      synchronized (listeners) {
        for (LlamaAMListener listener : listeners) {
          try {
            listener.onEvent(event);
          } catch (Throwable ex) {
            LOG.warn("listener.handle() error: {}", ex.toString(), ex);
          }
        }
      }
    }
  }

}

@@ -1270,21 +1270,24 @@
 private 
-final
+volatile
  Set%3CLla
@@ -1482,37 +1482,82 @@
 ener) %7B%0A    
-synchronized 
+Set%3CLlamaAMListener%3E newSet = new HashSet%3CLlamaAMListener%3E
 (listeners) 
@@ -1555,34 +1555,28 @@
 steners)
- %7B
+;
 %0A    
-  listeners
+newSet
 .add(lis
@@ -1579,33 +1579,51 @@
 (listener);%0A    
-%7D
+listeners = newSet;
 %0A  %7D%0A%0A  public v
@@ -1677,50 +1677,89 @@
     
-synchronized (listeners) %7B%0A      listeners
+Set%3CLlamaAMListener%3E newSet = new HashSet%3CLlamaAMListener%3E(listeners);%0A    newSet
 .rem
@@ -1773,25 +1773,43 @@
 tener);%0A    
-%7D
+listeners = newSet;
 %0A  %7D%0A%0A  prot
@@ -1882,43 +1882,8 @@
 ) %7B%0A
-      synchronized (listeners) %7B%0A  
     
@@ -1941,18 +1941,14 @@
     
-  
 try %7B%0A
-  
     
@@ -1986,18 +1986,16 @@
         
- 
- 
 %7D catch 
@@ -2007,26 +2007,24 @@
 wable ex) %7B%0A
-  
           LO
@@ -2085,20 +2085,8 @@
 x);%0A
-          %7D%0A
     

Remove syncronization from LlamaAM event dispatching