Skip to content

Commit

Permalink
preload common pool - issue #198 (#279)
Browse files Browse the repository at this point in the history
* enable to preload common pool
* move to java 8
* fix build - weird java version was upgrade to j8 but not animal sniffer
  • Loading branch information
rmannibucau authored Apr 19, 2022
1 parent 3b7aea6 commit 9c28f9d
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@
</signature>
<ignores>
<ignore>java.lang.invoke.MethodHandle</ignore>
<ignore>java.util.concurrent.ForkJoinPool</ignore>
</ignores>
</configuration>
</plugin>
Expand Down
56 changes: 56 additions & 0 deletions src/main/java/org/codehaus/mojo/exec/ExecJavaMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ForkJoinPool;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.factory.ArtifactFactory;
Expand Down Expand Up @@ -68,6 +71,18 @@ public class ExecJavaMojo
@Parameter( required = true, property = "exec.mainClass" )
private String mainClass;


/**
* Forces the creation of fork join common pool to avoids the threads to be owned by the isolated thread group
* and prevent a proper shutdown.
* If set to zero the default parallelism is used to precreate all threads,
* if negative it is ignored else the value is the one used to create the fork join threads.
*
* @since 3.0.1
*/
@Parameter( property = "exec.preloadCommonPool", defaultValue = "0" )
private int preloadCommonPool;

/**
* The class arguments.
*
Expand Down Expand Up @@ -234,6 +249,11 @@ public void execute()
getLog().debug( msg );
}

if ( preloadCommonPool >= 0 )
{
preloadCommonPool();
}

IsolatedThreadGroup threadGroup = new IsolatedThreadGroup( mainClass /* name */ );
Thread bootstrapThread = new Thread( threadGroup, new Runnable()
{
Expand Down Expand Up @@ -338,6 +358,42 @@ public void run()
registerSourceRoots();
}

/**
* To avoid the exec:java to consider common pool threads leaked, let's pre-create them.
*/
private void preloadCommonPool()
{
try
{
// ensure common pool exists in the jvm
final ExecutorService es = ForkJoinPool.commonPool();
final int max = preloadCommonPool > 0
? preloadCommonPool :
ForkJoinPool.getCommonPoolParallelism();
final CountDownLatch preLoad = new CountDownLatch( 1 );
for ( int i = 0;
i < max;
i++ )
{
es.submit(() -> {
try
{
preLoad.await();
}
catch ( InterruptedException e )
{
Thread.currentThread().interrupt();
}
});
}
preLoad.countDown();
}
catch (final Exception e)
{
getLog().debug(e.getMessage() + ", skipping commonpool earger init");
}
}

/**
* a ThreadGroup to isolate execution and collect exceptions.
*/
Expand Down

0 comments on commit 9c28f9d

Please sign in to comment.