From 8a2631696a56ba3dfd59c6b6223a57371a3fe505 Mon Sep 17 00:00:00 2001 From: Samuel Tardieu Date: Mon, 4 Jun 2012 20:56:46 +0200 Subject: Make the cache cache synchronized Concurrent modifications could occur, especially on very large displays where we may have to draw a lot of caches at the same time while fetching others. This could lead to ConcurrentModificationException errors, as described in #1706. --- main/src/cgeo/geocaching/CacheCache.java | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) (limited to 'main/src') diff --git a/main/src/cgeo/geocaching/CacheCache.java b/main/src/cgeo/geocaching/CacheCache.java index e8280e2..877c960 100644 --- a/main/src/cgeo/geocaching/CacheCache.java +++ b/main/src/cgeo/geocaching/CacheCache.java @@ -27,7 +27,7 @@ public class CacheCache { cachesCache.setRemoveHandler(new CacheRemoveHandler()); } - public void removeAllFromCache() { + public synchronized void removeAllFromCache() { cachesCache.clear(); } @@ -39,7 +39,9 @@ public class CacheCache { if (StringUtils.isBlank(geocode)) { throw new IllegalArgumentException("geocode must not be empty"); } - cachesCache.remove(geocode); + synchronized(this) { + cachesCache.remove(geocode); + } } /** @@ -56,8 +58,10 @@ public class CacheCache { if (StringUtils.isBlank(cache.getGeocode())) { throw new IllegalArgumentException("geocode must not be empty"); } - cache.addStorageLocation(StorageLocation.CACHE); - cachesCache.put(cache.getGeocode(), cache); + synchronized(this) { + cache.addStorageLocation(StorageLocation.CACHE); + cachesCache.put(cache.getGeocode(), cache); + } } /** @@ -69,10 +73,12 @@ public class CacheCache { if (StringUtils.isBlank(geocode)) { throw new IllegalArgumentException("geocode must not be empty"); } - return cachesCache.get(geocode); + synchronized(this) { + return cachesCache.get(geocode); + } } - public Set getInViewport(final Viewport viewport, final CacheType cacheType) { + public synchronized Set getInViewport(final Viewport viewport, final CacheType cacheType) { final Set geocodes = new HashSet(); for (final cgCache cache : cachesCache.values()) { if (cache.getCoords() == null) { @@ -89,7 +95,7 @@ public class CacheCache { } @Override - public String toString() { + public synchronized String toString() { return StringUtils.join(cachesCache.keySet(), ' '); } -- cgit v1.1