From 5b22e0bb4c52b46d99ac78c175e9d5148e583461 Mon Sep 17 00:00:00 2001 From: Mikhail Titov Date: Tue, 16 Jan 2024 23:56:46 -0600 Subject: [PATCH] Ensure we do a final universe update There is a very high chance 3D updater gets a clean shut down between task invocations. Let's call it one final time more. This fixes #14 --- .../java/ini/trakem2/display/Display3D.java | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/src/main/java/ini/trakem2/display/Display3D.java b/src/main/java/ini/trakem2/display/Display3D.java index 4769550e..189bfc31 100644 --- a/src/main/java/ini/trakem2/display/Display3D.java +++ b/src/main/java/ini/trakem2/display/Display3D.java @@ -450,28 +450,26 @@ public Vector> call() { setWaitingCursor(); - // Start new scheduler to publish/add meshes to the 3D Viewer every 5 seconds and when done. + // Start new scheduler to publish/add meshes to the 3D Viewer every 4 seconds and when done. final Hashtable> contents = new Hashtable>(); final ScheduledExecutorService updater = Executors.newScheduledThreadPool(1); final AtomicInteger counter = new AtomicInteger(); - updater.scheduleWithFixedDelay(new Runnable() { - @Override - public void run() { - // Obtain a copy of the contents queue - final HashMap> m = new HashMap>(); - synchronized (contents) { - m.putAll(contents); - contents.clear(); - } - if (m.isEmpty()) return; - // Add all to the corresponding Display3D - for (final Map.Entry> e : m.entrySet()) { - e.getKey().universe.addContentLater(e.getValue()); - counter.getAndAdd(e.getValue().size()); - } - Utils.showStatus(new StringBuilder("Rendered ").append(counter.get()).append('/').append(hs.size()).toString()); + Runnable updaterTask = () -> { + // Obtain a copy of the contents queue + final HashMap> m = new HashMap>(); + synchronized (contents) { + m.putAll(contents); + contents.clear(); } - }, 100, 4000, TimeUnit.MILLISECONDS); + if (m.isEmpty()) return; + // Add all to the corresponding Display3D + for (final Map.Entry> e : m.entrySet()) { + e.getKey().universe.addContentLater(e.getValue()); + counter.getAndAdd(e.getValue().size()); + } + Utils.showStatus(new StringBuilder("Rendered ").append(counter.get()).append('/').append(hs.size()).toString()); + }; + updater.scheduleWithFixedDelay(updaterTask, 100, 4000, TimeUnit.MILLISECONDS); // A list of all generated Content objects final Vector> list = new Vector>(); @@ -556,14 +554,10 @@ public void run() { IJError.print(t); } } - try { - // Shutdown scheduler and execute remaining tasks - for (final Runnable r : updater.shutdownNow()) { - r.run(); - } - } catch (final Throwable e) { - IJError.print(e); - } + // Shutdown scheduler + updater.shutdown(); + // …and run once more in case we were in a delay between calls + updaterTask.run(); // Reset cursor doneWaiting(); Utils.showStatus(new StringBuilder("Done rendering ").append(counter.get()).append('/').append(hs.size()).toString());