Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Kill Zombies from the Fractal Dimension #638

Merged
merged 2 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/main/java/net/imagej/ops/morphology/outline/Outline.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static java.util.Arrays.stream;

Expand Down Expand Up @@ -133,6 +134,29 @@ public void compute(final RandomAccessibleInterval<B> input,
logService.error(e);
}
}
shutdownAndAwaitTermination(pool);
}

// Shuts down an ExecutorService as per recommended by Oracle
private void shutdownAndAwaitTermination(final ExecutorService executor) {
executor.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
executor.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
logService.trace("Pool did not terminate");
}
}
}
catch (final InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
executor.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
logService.trace(ie);
}
}

/**
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/net/imagej/ops/topology/BoxCount.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.stream.LongStream;
import java.util.stream.Stream;

Expand All @@ -48,6 +49,7 @@
import net.imglib2.type.numeric.real.DoubleType;
import net.imglib2.util.ValuePair;

import org.scijava.log.LogService;
import org.scijava.plugin.Parameter;
import org.scijava.plugin.Plugin;

Expand Down Expand Up @@ -77,6 +79,9 @@ public class BoxCount<B extends BooleanType<B>> extends
implements Ops.Topology.BoxCount
{

@Parameter
private LogService logService;

/** Starting size of the boxes in pixels */
@Parameter(required = false, persist = false)
private Long maxSize = 48L;
Expand Down Expand Up @@ -237,8 +242,31 @@ private long countForegroundBoxes(final RandomAccessibleInterval<B> input,
e.printStackTrace();
}
}
shutdownAndAwaitTermination(pool);
return foregroundBoxes;
}

// Shuts down an ExecutorService as per recommended by Oracle
private void shutdownAndAwaitTermination(final ExecutorService executor) {
executor.shutdown(); // Disable new tasks from being submitted
try {
// Wait a while for existing tasks to terminate
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
executor.shutdownNow(); // Cancel currently executing tasks
// Wait a while for tasks to respond to being cancelled
if (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
logService.trace("Pool did not terminate");
}
}
}
catch (final InterruptedException ie) {
// (Re-)Cancel if current thread also interrupted
executor.shutdownNow();
// Preserve interrupt status
Thread.currentThread().interrupt();
logService.trace(ie);
}
}

/**
* Creates a task for parallel calculation of foreground boxes.
Expand Down