1616import net .imglib2 .RandomAccess ;
1717import net .imglib2 .RandomAccessible ;
1818import net .imglib2 .RandomAccessibleInterval ;
19- import net .imglib2 .type .Type ;
20- import net .imglib2 .type .numeric .RealType ;
21- import net .imglib2 .type .numeric .real .DoubleType ;
22- import net .imglib2 .type .numeric .real .FloatType ;
2319import net .imglib2 .util .IntervalIndexer ;
2420import net .imglib2 .util .Intervals ;
2521import net .imglib2 .view .Views ;
@@ -83,10 +79,15 @@ protected void process( final RandomAccessible< ? extends T > source, final Rand
8379 final long [] dim = Intervals .dimensionsAsLongArray ( target );
8480 dim [ direction ] = 1 ;
8581
86- final int numTasks = numThreads > 1 ? numThreads * 4 : 1 ;
82+ final int numTasks = numThreads > 1 ? timesFourAvoidOverflow ( numThreads ) : 1 ;
8783 LineConvolution .forEachIntervalElementInParallel ( executorService , numTasks , new FinalInterval ( dim ), actionFactory );
8884 }
8985
86+ private int timesFourAvoidOverflow ( int x )
87+ {
88+ return (int ) Math .min ((long ) x * 4 , Integer .MAX_VALUE );
89+ }
90+
9091 /**
9192 * {@link #forEachIntervalElementInParallel(ExecutorService, int, Interval, Supplier)}
9293 * executes a given action for each position in a given interval. Therefor
@@ -110,14 +111,14 @@ public static void forEachIntervalElementInParallel( final ExecutorService servi
110111 final long [] min = Intervals .minAsLongArray ( interval );
111112 final long [] dim = Intervals .dimensionsAsLongArray ( interval );
112113 final long size = Intervals .numElements ( dim );
113- final long endIndex = size ;
114- final long taskSize = ( size + numTasks - 1 ) / numTasks ; // round up
114+ final int boundedNumTasks = ( int ) Math . max ( 1 , Math . min ( size , numTasks )) ;
115+ final long taskSize = ( size - 1 ) / boundedNumTasks + 1 ; // taskSize = roundUp(size / boundedNumTasks);
115116 final ArrayList < Callable < Void > > callables = new ArrayList <>();
116117
117- for ( int taskNum = 0 ; taskNum < numTasks ; ++taskNum )
118+ for ( int taskNum = 0 ; taskNum < boundedNumTasks ; ++taskNum )
118119 {
119120 final long myStartIndex = taskNum * taskSize ;
120- final long myEndIndex = Math .min ( endIndex , myStartIndex + taskSize );
121+ final long myEndIndex = Math .min ( size , myStartIndex + taskSize );
121122 final Callable < Void > r = () -> {
122123 final Consumer < Localizable > action = actionFactory .get ();
123124 final long [] position = new long [ dim .length ];
0 commit comments