3939import java .util .Arrays ;
4040import java .util .Collections ;
4141import java .util .Iterator ;
42- import java .util .List ;
4342import java .util .Set ;
44- import java .util .concurrent .ExecutionException ;
4543import java .util .concurrent .ExecutorService ;
46- import java .util .concurrent .Executors ;
47- import java .util .concurrent .Future ;
4844
4945import net .imglib2 .Cursor ;
5046import net .imglib2 .FinalInterval ;
5147import net .imglib2 .RandomAccess ;
5248import net .imglib2 .RandomAccessible ;
5349import net .imglib2 .RandomAccessibleInterval ;
5450import net .imglib2 .iterator .IntervalIterator ;
51+ import net .imglib2 .parallel .Parallelization ;
5552import net .imglib2 .roi .labeling .ImgLabeling ;
5653import net .imglib2 .type .numeric .IntegerType ;
5754import net .imglib2 .view .Views ;
@@ -99,17 +96,19 @@ public CollectNeighborLabelsFactory getFactory()
9996 * @param se
10097 * structuring element to use. 8-connected or 4-connected
10198 * (respectively n-dimensional analog)
99+ * @param service
100+ * service providing threads for multi-threading
102101 */
103102 public static < T extends IntegerType < T >, L , I extends IntegerType < I > > void labelAllConnectedComponents (
104103 final RandomAccessible < T > input ,
105104 final ImgLabeling < L , I > labeling ,
106105 final Iterator < L > labelGenerator ,
107- final StructuringElement se )
106+ final StructuringElement se ,
107+ final ExecutorService service )
108108 {
109- final int numThreads = Runtime .getRuntime ().availableProcessors ();
110- final ExecutorService service = Executors .newFixedThreadPool ( numThreads );
111- labelAllConnectedComponents ( input , labeling , labelGenerator , se , service );
112- service .shutdown ();
109+ Parallelization .runWithExecutor ( service ,
110+ () -> labelAllConnectedComponents ( input , labeling , labelGenerator , se )
111+ );
113112 }
114113
115114 /**
@@ -130,21 +129,18 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
130129 * @param se
131130 * structuring element to use. 8-connected or 4-connected
132131 * (respectively n-dimensional analog)
133- * @param service
134- * service providing threads for multi-threading
135132 */
136133 public static < T extends IntegerType < T >, L , I extends IntegerType < I > > void labelAllConnectedComponents (
137134 final RandomAccessible < T > input ,
138135 final ImgLabeling < L , I > labeling ,
139136 final Iterator < L > labelGenerator ,
140- final StructuringElement se ,
141- final ExecutorService service )
137+ final StructuringElement se )
142138 {
143139 final RandomAccessibleInterval < I > output = labeling .getIndexImg ();
144140 for ( final I i : Views .iterable ( output ) )
145141 i .setZero ();
146142
147- final int numLabels = labelAllConnectedComponents ( input , output , se , service ) + 1 ;
143+ final int numLabels = labelAllConnectedComponents ( input , output , se ) + 1 ;
148144
149145 final ArrayList < Set < L > > labelSets = new ArrayList <>();
150146 labelSets .add ( Collections .emptySet () );
@@ -161,7 +157,6 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
161157 *
162158 * <p>
163159 * <em>Note, that the {@code output} image must be cleared to 0!</em>
164- * </p>
165160 *
166161 * @param input
167162 * input image with pixels > 0 belonging to foreground.
@@ -170,19 +165,20 @@ public static < T extends IntegerType< T >, L, I extends IntegerType< I > > void
170165 * @param se
171166 * structuring element to use. 8-connected or 4-connected
172167 * (respectively n-dimensional analog)
168+ * @param service
169+ * service providing threads for multi-threading
173170 * @return the number of connected components (that is, the highest value
174171 * occurring in the output image.
175172 */
176173 public static < T extends IntegerType < T >, L extends IntegerType < L > > int labelAllConnectedComponents (
177174 final RandomAccessible < T > input ,
178175 final RandomAccessibleInterval < L > output ,
179- final StructuringElement se )
176+ final StructuringElement se ,
177+ final ExecutorService service )
180178 {
181- final int numThreads = Runtime .getRuntime ().availableProcessors ();
182- final ExecutorService service = Executors .newFixedThreadPool ( numThreads );
183- final int result = labelAllConnectedComponents ( input , output , se , service );
184- service .shutdown ();
185- return result ;
179+ return Parallelization .runWithExecutor ( service ,
180+ () -> labelAllConnectedComponents ( input , output , se )
181+ );
186182 }
187183
188184 /**
@@ -192,6 +188,7 @@ public static < T extends IntegerType< T >, L extends IntegerType< L > > int lab
192188 *
193189 * <p>
194190 * <em>Note, that the {@code output} image must be cleared to 0!</em>
191+ * </p>
195192 *
196193 * @param input
197194 * input image with pixels > 0 belonging to foreground.
@@ -200,16 +197,13 @@ public static < T extends IntegerType< T >, L extends IntegerType< L > > int lab
200197 * @param se
201198 * structuring element to use. 8-connected or 4-connected
202199 * (respectively n-dimensional analog)
203- * @param service
204- * service providing threads for multi-threading
205200 * @return the number of connected components (that is, the highest value
206201 * occurring in the output image.
207202 */
208203 public static < T extends IntegerType < T >, L extends IntegerType < L > > int labelAllConnectedComponents (
209204 final RandomAccessible < T > input ,
210205 final RandomAccessibleInterval < L > output ,
211- final StructuringElement se ,
212- final ExecutorService service )
206+ final StructuringElement se )
213207 {
214208 final int n = output .numDimensions ();
215209 final int splitDim = n - 1 ;
@@ -234,37 +228,14 @@ public static < T extends IntegerType< T >, L extends IntegerType< L > > int lab
234228 min [ splitDim ] += taskSize ;
235229 }
236230
237- final ArrayList < Future < ? > > futures = new ArrayList < Future < ? > >();
238- for ( final Fragment < T , L > fragment : fragments )
239- {
240- futures .add ( service .submit ( new Runnable ()
241- {
242- @ Override
243- public void run ()
244- {
245- fragment .mark ();
246- }
247- } ) );
248- }
249- getAllFutures ( futures );
231+ Parallelization .getTaskExecutor ().forEach ( Arrays .asList ( fragments ), Fragment ::mark );
250232
251233 final TIntArrayList merged = mergeCanonicalLists ( fragments );
252234 for ( int i = 1 ; i < numTasks ; ++i )
253235 fragments [ i ].linkToPreviousFragment ( fragments [ i - 1 ], merged );
254236 final int numComponents = splitCanonicalLists ( fragments , merged );
255237
256- for ( final Fragment < T , L > fragment : fragments )
257- {
258- futures .add ( service .submit ( new Runnable ()
259- {
260- @ Override
261- public void run ()
262- {
263- fragment .relabel ();
264- }
265- } ) );
266- }
267- getAllFutures ( futures );
238+ Parallelization .getTaskExecutor ().forEach ( Arrays .asList ( fragments ), Fragment ::relabel );
268239
269240 return numComponents ;
270241 }
@@ -481,26 +452,6 @@ private static < T extends IntegerType< T >, L extends IntegerType< L > > int sp
481452 return nextLabel - 1 ;
482453 }
483454
484- private static void getAllFutures ( final List < Future < ? > > futures )
485- {
486- for ( final Future < ? > future : futures )
487- {
488- try
489- {
490- future .get ();
491- }
492- catch ( final InterruptedException e )
493- {
494- e .printStackTrace ();
495- }
496- catch ( final ExecutionException e )
497- {
498- e .printStackTrace ();
499- }
500- }
501- futures .clear ();
502- }
503-
504455 private static interface CollectNeighborLabels < L extends IntegerType < L > >
505456 {
506457 public void collect ( RandomAccess < L > la , final TIntArrayList neighborLabels , final long [] labelsMin , final long [] labelsMax );
0 commit comments