Skip to content

Commit

Permalink
Use the QuarkusForkJoinWorkerThreadFactory for fork-join pools
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanne committed Sep 15, 2021
1 parent 5d77d27 commit b274f6c
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.quarkus.deployment;

import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.SystemPropertyBuildItem;

public final class ForkJoinPoolProcessor {

@BuildStep
SystemPropertyBuildItem setProperty() {
return new SystemPropertyBuildItem("java.util.concurrent.ForkJoinPool.common.threadFactory",
"io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,8 @@ protected void prepare() throws Exception {
devModeContext.getAdditionalModules().addAll(dependencies);

args.add("-Djava.util.logging.manager=org.jboss.logmanager.LogManager");
args.add(
"-Djava.util.concurrent.ForkJoinPool.common.threadFactory=io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory");

File tempFile = new File(buildDir, applicationName + "-dev.jar");
tempFile.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public void accept(BuildChainBuilder builder) {
resultMap.put("files", result);
List javaargs = new ArrayList<String>();
javaargs.add("-Djava.util.logging.manager=org.jboss.logmanager.LogManager");
javaargs.add(
"-Djava.util.concurrent.ForkJoinPool.common.threadFactory=io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory");
resultMap.put("java-args", javaargs);
resultMap.put("main-class", buildResult.consume(MainClassBuildItem.class).getClassName());
if (nativeRequested) {
Expand Down
3 changes: 3 additions & 0 deletions core/runtime/src/main/java/io/quarkus/runtime/Quarkus.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.jboss.logging.Logger;
import org.jboss.logmanager.LogManager;

import io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory;
import io.quarkus.launcher.QuarkusLauncher;

/**
Expand Down Expand Up @@ -58,6 +59,8 @@ public static void run(Class<? extends QuarkusApplication> quarkusApplication, B
String... args) {
try {
System.setProperty("java.util.logging.manager", LogManager.class.getName());
System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory",
QuarkusForkJoinWorkerThreadFactory.class.getName());
//production and common dev mode path
//we already have an application, run it directly
Class<? extends Application> appClass = (Class<? extends Application>) Class.forName(Application.APP_CLASS_NAME,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.bootstrap.forkjoin;

import io.quarkus.bootstrap.runner.RunnerClassLoader;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;

public class QuarkusForkJoinWorkerThread extends ForkJoinWorkerThread {

private static volatile ClassLoader qClassloader;

protected QuarkusForkJoinWorkerThread(ForkJoinPool pool) {
super(pool);
}

public static synchronized void setQuarkusAppClassloader(RunnerClassLoader runnerClassLoader) {
if (qClassloader != null) {
throw new IllegalStateException("Attempting to set the Quarkus root classloader while it was already set");
}
if (runnerClassLoader == null) {
throw new IllegalStateException("Attempting to set the Quarkus root classloader to null");
}
qClassloader = runnerClassLoader;
}

protected void onStart() {
super.onStart();
if (qClassloader == null) {
throw new IllegalStateException(
"Fork Join pool initialization has been triggered before the primary classloader has been initialized. " +
"Use this stacktrace to figure out what triggered it, and avoid using the Fork Join pool this early during the boot process.");
} else {
super.setContextClassLoader(qClassloader);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.bootstrap.forkjoin;

import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.ForkJoinWorkerThread;

public final class QuarkusForkJoinWorkerThreadFactory implements ForkJoinPool.ForkJoinWorkerThreadFactory {

public QuarkusForkJoinWorkerThreadFactory() {
//Needs a public constructor for the JDK to pick it up
}

@Override
public ForkJoinWorkerThread newThread(ForkJoinPool pool) {
return new QuarkusForkJoinWorkerThread(pool);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.bootstrap.runner;

import io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThread;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
Expand All @@ -21,6 +22,8 @@ public class QuarkusEntryPoint {

public static void main(String... args) throws Throwable {
System.setProperty("java.util.logging.manager", org.jboss.logmanager.LogManager.class.getName());
System.setProperty("java.util.concurrent.ForkJoinPool.common.threadFactory",
"io.quarkus.bootstrap.forkjoin.QuarkusForkJoinWorkerThreadFactory");
Timing.staticInitStarted(false);
doRun(args);
}
Expand All @@ -44,6 +47,7 @@ private static void doRun(Object args) throws IOException, ClassNotFoundExceptio
}
try {
Thread.currentThread().setContextClassLoader(app.getRunnerClassLoader());
QuarkusForkJoinWorkerThread.setQuarkusAppClassloader(app.getRunnerClassLoader());
Class<?> mainClass = app.getRunnerClassLoader().loadClass(app.getMainClass());
mainClass.getMethod("main", String[].class).invoke(null, args);
} finally {
Expand Down

0 comments on commit b274f6c

Please sign in to comment.