diff --git a/src/main/java/ch/epfl/biop/bdv/img/CacheControlOverride.java b/src/main/java/ch/epfl/biop/bdv/img/CacheControlOverride.java index afc40f6..bbad4b0 100644 --- a/src/main/java/ch/epfl/biop/bdv/img/CacheControlOverride.java +++ b/src/main/java/ch/epfl/biop/bdv/img/CacheControlOverride.java @@ -21,9 +21,28 @@ */ package ch.epfl.biop.bdv.img; +import bdv.cache.SharedQueue; import bdv.img.cache.VolatileGlobalCellCache; +import java.lang.reflect.Field; + public interface CacheControlOverride { void setCacheControl(VolatileGlobalCellCache cache); + + public static class Tools { + public static void shutdownCacheQueue(VolatileGlobalCellCache cache) { + try { + Field queueField = VolatileGlobalCellCache.class.getDeclaredField( + "queue"); + queueField.setAccessible(true); + SharedQueue queue = (SharedQueue) queueField.get(cache); + queue.shutdown(); // Kill the non-used thread + } catch (NoSuchFieldException e) { + throw new RuntimeException(e); + } catch (IllegalAccessException e) { + throw new RuntimeException(e); + } + } + } } diff --git a/src/main/java/ch/epfl/biop/bdv/img/OpenersImageLoader.java b/src/main/java/ch/epfl/biop/bdv/img/OpenersImageLoader.java index bb57fe3..23ef724 100644 --- a/src/main/java/ch/epfl/biop/bdv/img/OpenersImageLoader.java +++ b/src/main/java/ch/epfl/biop/bdv/img/OpenersImageLoader.java @@ -35,6 +35,7 @@ import java.io.Closeable; import java.io.IOException; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -230,6 +231,7 @@ public void close() { @Override public void setCacheControl(VolatileGlobalCellCache cache) { + CacheControlOverride.Tools.shutdownCacheQueue(this.cache); this.cache.clearCache(); this.cache = cache; } diff --git a/src/main/java/ch/epfl/biop/bdv/img/imageplus/ImagePlusImageLoader.java b/src/main/java/ch/epfl/biop/bdv/img/imageplus/ImagePlusImageLoader.java index bc939e0..f080ff6 100644 --- a/src/main/java/ch/epfl/biop/bdv/img/imageplus/ImagePlusImageLoader.java +++ b/src/main/java/ch/epfl/biop/bdv/img/imageplus/ImagePlusImageLoader.java @@ -187,6 +187,7 @@ public VolatileGlobalCellCache getCacheControl() { @Override public void setCacheControl(VolatileGlobalCellCache cache) { + CacheControlOverride.Tools.shutdownCacheQueue(this.cache); this.cache.clearCache(); this.cache = cache; }