From 40423592243a110cef911cf89f0520c50092f709 Mon Sep 17 00:00:00 2001
From: Tobias Pietzsch <tobias.pietzsch@gmail.com>
Date: Sun, 2 Mar 2014 11:26:23 -0500
Subject: [PATCH] only a part of VolatileGlobalCellCache.createGlobal() needs
 to be synchronized

---
 .../img/cache/VolatileGlobalCellCache.java    | 21 +++++++++++--------
 1 file changed, 12 insertions(+), 9 deletions(-)

diff --git a/src/main/java/bdv/img/cache/VolatileGlobalCellCache.java b/src/main/java/bdv/img/cache/VolatileGlobalCellCache.java
index a1a38632..08a7c956 100644
--- a/src/main/java/bdv/img/cache/VolatileGlobalCellCache.java
+++ b/src/main/java/bdv/img/cache/VolatileGlobalCellCache.java
@@ -407,20 +407,23 @@ public class VolatileGlobalCellCache< A extends VolatileAccess > implements Cach
 	 *
 	 * @return a cell with the specified coordinates.
 	 */
-	public synchronized VolatileCell< A > createGlobal( final int[] cellDims, final long[] cellMin, final int timepoint, final int setup, final int level, final int index, final LoadingStrategy loadingStrategy )
+	public VolatileCell< A > createGlobal( final int[] cellDims, final long[] cellMin, final int timepoint, final int setup, final int level, final int index, final LoadingStrategy loadingStrategy )
 	{
 		final Key k = new Key( timepoint, setup, level, index );
 		Entry entry = null;
 
-		final Reference< Entry > ref = softReferenceCache.get( k );
-		if ( ref != null )
-			entry = ref.get();
-
-		if ( entry == null )
+		synchronized ( softReferenceCache )
 		{
-			final VolatileCell< A > cell = new VolatileCell< A >( cellDims, cellMin, loader.emptyArray( cellDims ) );
-			entry = new Entry( k, cell );
-			softReferenceCache.put( k, new WeakReference< Entry >( entry ) );
+			final Reference< Entry > ref = softReferenceCache.get( k );
+			if ( ref != null )
+				entry = ref.get();
+
+			if ( entry == null )
+			{
+				final VolatileCell< A > cell = new VolatileCell< A >( cellDims, cellMin, loader.emptyArray( cellDims ) );
+				entry = new Entry( k, cell );
+				softReferenceCache.put( k, new WeakReference< Entry >( entry ) );
+			}
 		}
 
 		switch ( loadingStrategy )
-- 
GitLab