Skip to content

Commit af9bafc

Browse files
committed
AbstractMultiThreadedConvolution: fix issue 77 (part 1/2)
See #77 Maximum pool size for Executors.newCachedThreadPool() is Integer.MAX_VALUE. This is not useful, when you want to decide, on how many threads a task should be distributed. Use the number of available processor as an upper limit in that case.
1 parent b4b2c1a commit af9bafc

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

src/main/java/net/imglib2/algorithm/convolution/AbstractMultiThreadedConvolution.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ final public void process( final RandomAccessible< ? extends T > source, final R
3939
{
4040
if ( executor == null )
4141
{
42-
final int numThreads = suggestNumThreads();
42+
final int numThreads = Runtime.getRuntime().availableProcessors();
4343
final ExecutorService executor = Executors.newFixedThreadPool( numThreads );
4444
try
4545
{
@@ -56,15 +56,13 @@ final public void process( final RandomAccessible< ? extends T > source, final R
5656
}
5757
}
5858

59-
private int getNumThreads( final ExecutorService executor )
59+
static int getNumThreads( final ExecutorService executor )
6060
{
61-
if ( executor instanceof ThreadPoolExecutor )
62-
return ( ( ThreadPoolExecutor ) executor ).getMaximumPoolSize();
63-
return suggestNumThreads();
61+
int maxPoolSize = ( executor instanceof ThreadPoolExecutor ) ?
62+
( ( ThreadPoolExecutor ) executor ).getMaximumPoolSize() :
63+
Integer.MAX_VALUE;
64+
int availableProcessors = Runtime.getRuntime().availableProcessors();
65+
return Math.max(1, Math.min(availableProcessors, maxPoolSize));
6466
}
6567

66-
private int suggestNumThreads()
67-
{
68-
return Runtime.getRuntime().availableProcessors();
69-
}
7068
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.imglib2.algorithm.convolution;
2+
3+
import java.util.concurrent.ExecutorService;
4+
import java.util.concurrent.Executors;
5+
6+
import org.junit.Test;
7+
8+
import static junit.framework.TestCase.assertTrue;
9+
import static org.junit.Assert.assertArrayEquals;
10+
import static org.junit.Assert.assertEquals;
11+
import static org.junit.Assert.assertNotEquals;
12+
13+
/**
14+
* Tests {@link AbstractMultiThreadedConvolution}
15+
*
16+
* @author Matthias Arzt
17+
*/
18+
public class AbstractMultiThreadedConvolutionTest
19+
{
20+
@Test
21+
public void testSuggestNumTasksFixedThreadPool() {
22+
if ( Runtime.getRuntime().availableProcessors() < 3 )
23+
return;
24+
final ExecutorService executor = Executors.newFixedThreadPool( 3 );
25+
int result = AbstractMultiThreadedConvolution.getNumThreads( executor );
26+
assertEquals( 3, result );
27+
}
28+
29+
@Test
30+
public void testSuggestNumTasksCachedThreadPool() {
31+
final ExecutorService executor = Executors.newCachedThreadPool();
32+
int result = AbstractMultiThreadedConvolution.getNumThreads( executor );
33+
assertTrue( Runtime.getRuntime().availableProcessors() >= result );
34+
}
35+
}

0 commit comments

Comments
 (0)