Switch to a synchronized list from a copy-on-write list
Turns out, we only want one thread at a time writing
to the list, and we were correctly using the list as
if it were synchronized. Might as well embrace that.
import org.openqa.selenium.remote.server.jmx.ManagedAttribute;
import org.openqa.selenium.remote.server.jmx.ManagedService;
+import java.util.ArrayList;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
-import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Logger;
import javax.management.MalformedObjectNameException;
@@ -114,8 +115,7 @@ public HtmlRenderer getHtmlRender() {
private volatile boolean down = false;
private volatile boolean poll = true;
- // TODO freynaud
- private List<RemoteException> errors = new CopyOnWriteArrayList<>();
+ private List<RemoteException> errors = Collections.synchronizedList(new ArrayList<>());
private Thread pollingThread = null;
@ManagedAttribute
@@ -175,9 +175,10 @@ public void stopPolling() {
}
public void addNewEvent(RemoteException event) {
+ synchronized (errors) {
errors.add(event);
onEvent(errors, event);
-
+ }
}
public void onEvent(List<RemoteException> events, RemoteException lastInserted) {
@@ -185,6 +186,7 @@ public void onEvent(List<RemoteException> events, RemoteException lastInserted)
if (e instanceof RemoteNotReachableException) {
LOG.info(e.getMessage());
down = true;
+ // We are already in a synchronized block, so do not need to synchronize again
this.errors.clear();
}
if (e instanceof RemoteUnregisterException) {
