Skip to content

Commit 79713c7

Browse files
committed
shutdown queue through reflection when overriding cache, related: bigdataviewer/bigdataviewer-core#137
1 parent 85d226c commit 79713c7

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

src/main/java/ch/epfl/biop/bdv/img/CacheControlOverride.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,28 @@
2121
*/
2222
package ch.epfl.biop.bdv.img;
2323

24+
import bdv.cache.SharedQueue;
2425
import bdv.img.cache.VolatileGlobalCellCache;
2526

27+
import java.lang.reflect.Field;
28+
2629
public interface CacheControlOverride {
2730

2831
void setCacheControl(VolatileGlobalCellCache cache);
32+
33+
public static class Tools {
34+
public static void shutdownCacheQueue(VolatileGlobalCellCache cache) {
35+
try {
36+
Field queueField = VolatileGlobalCellCache.class.getDeclaredField(
37+
"queue");
38+
queueField.setAccessible(true);
39+
SharedQueue queue = (SharedQueue) queueField.get(cache);
40+
queue.shutdown(); // Kill the non-used thread
41+
} catch (NoSuchFieldException e) {
42+
throw new RuntimeException(e);
43+
} catch (IllegalAccessException e) {
44+
throw new RuntimeException(e);
45+
}
46+
}
47+
}
2948
}

src/main/java/ch/epfl/biop/bdv/img/OpenersImageLoader.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535

3636
import java.io.Closeable;
3737
import java.io.IOException;
38+
import java.lang.reflect.Field;
3839
import java.util.ArrayList;
3940
import java.util.HashMap;
4041
import java.util.List;
@@ -230,6 +231,7 @@ public void close() {
230231

231232
@Override
232233
public void setCacheControl(VolatileGlobalCellCache cache) {
234+
CacheControlOverride.Tools.shutdownCacheQueue(this.cache);
233235
this.cache.clearCache();
234236
this.cache = cache;
235237
}

src/main/java/ch/epfl/biop/bdv/img/imageplus/ImagePlusImageLoader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ public VolatileGlobalCellCache getCacheControl() {
187187

188188
@Override
189189
public void setCacheControl(VolatileGlobalCellCache cache) {
190+
CacheControlOverride.Tools.shutdownCacheQueue(this.cache);
190191
this.cache.clearCache();
191192
this.cache = cache;
192193
}

0 commit comments

Comments
 (0)