diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2012-06-04 20:56:46 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2012-06-04 20:56:46 +0200 |
commit | 8a2631696a56ba3dfd59c6b6223a57371a3fe505 (patch) | |
tree | 50d149f3d1c8b64c33fdedf9e48bc09f4709e03c /main/src | |
parent | cb5a5b2a3afee2f09d8a9c48507f85f0053520a6 (diff) | |
download | cgeo-8a2631696a56ba3dfd59c6b6223a57371a3fe505.zip cgeo-8a2631696a56ba3dfd59c6b6223a57371a3fe505.tar.gz cgeo-8a2631696a56ba3dfd59c6b6223a57371a3fe505.tar.bz2 |
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.
Diffstat (limited to 'main/src')
-rw-r--r-- | main/src/cgeo/geocaching/CacheCache.java | 20 |
1 files changed, 13 insertions, 7 deletions
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<String> getInViewport(final Viewport viewport, final CacheType cacheType) { + public synchronized Set<String> getInViewport(final Viewport viewport, final CacheType cacheType) { final Set<String> geocodes = new HashSet<String>(); 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(), ' '); } |