Skip to content

Commit

Permalink
Ensure we do a final universe update
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mlt committed Jan 17, 2024
1 parent 7b39b73 commit 5b22e0b
Showing 1 changed file with 20 additions and 26 deletions.
46 changes: 20 additions & 26 deletions src/main/java/ini/trakem2/display/Display3D.java
Original file line number Diff line number Diff line change
Expand Up @@ -450,28 +450,26 @@ public Vector<Future<Content>> 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<Display3D,Vector<Content>> contents = new Hashtable<Display3D,Vector<Content>>();
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<Display3D,Vector<Content>> m = new HashMap<Display3D,Vector<Content>>();
synchronized (contents) {
m.putAll(contents);
contents.clear();
}
if (m.isEmpty()) return;
// Add all to the corresponding Display3D
for (final Map.Entry<Display3D,Vector<Content>> 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<Display3D,Vector<Content>> m = new HashMap<Display3D,Vector<Content>>();
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<Display3D,Vector<Content>> 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<Future<Content>> list = new Vector<Future<Content>>();
Expand Down Expand Up @@ -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());
Expand Down

0 comments on commit 5b22e0b

Please sign in to comment.