diff --git a/src/main/java/org/openstreetmap/josm/plugins/mapillary/model/ImageDetection.java b/src/main/java/org/openstreetmap/josm/plugins/mapillary/model/ImageDetection.java index 80d61130f..ea2cf6326 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/mapillary/model/ImageDetection.java +++ b/src/main/java/org/openstreetmap/josm/plugins/mapillary/model/ImageDetection.java @@ -55,7 +55,8 @@ public class ImageDetection extends SpecialImage .getCache(CACHE_NAME_PREFIX + "instances"); private static final String PACKAGE_TRAFFIC_SIGNS = "trafficsign"; - private static final List>>> searchDetections = new ArrayList<>(3); + private static final List>>> searchDetections = Collections + .synchronizedList(new ArrayList<>(3)); private static void setSearchDetections() { // segmentations take awhile to get (10+ seconds on my machine), so default off @@ -118,17 +119,19 @@ public static void getDetections(String key, BiConsumer> getDetections(String key, boolean wait) { List> detections = new ArrayList<>(); - for (CacheAccess>> cache : searchDetections) { - final List> layerDetections; - if (wait) { - layerDetections = cache.get(key, - () -> getDetections(key, cache.getCacheAttributes().getCacheName().replaceFirst(CACHE_NAME_PREFIX, ""))); - } else if (key != null && cache.get(key) != null) { - layerDetections = cache.get(key); - } else { - layerDetections = Collections.emptyList(); + synchronized (searchDetections) { + for (CacheAccess>> cache : searchDetections) { + final List> layerDetections; + if (wait) { + layerDetections = cache.get(key, + () -> getDetections(key, cache.getCacheAttributes().getCacheName().replaceFirst(CACHE_NAME_PREFIX, ""))); + } else if (key != null && cache.get(key) != null) { + layerDetections = cache.get(key); + } else { + layerDetections = Collections.emptyList(); + } + detections.addAll(layerDetections); } - detections.addAll(layerDetections); } return detections.isEmpty() ? Collections.emptyList() : detections; }