Skip to content

Commit

Permalink
Merge pull request #638 from bonej-org/master
Browse files Browse the repository at this point in the history
Kill Zombies from the Fractal Dimension
  • Loading branch information
ctrueden authored Aug 13, 2021
2 parents e810d36 + d00da72 commit fa7f03e
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
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

0 comments on commit fa7f03e

Please sign in to comment.