Skip to content

Commit

Permalink
Fixes #8007 - Support Loom. (#8465)
Browse files Browse the repository at this point in the history
Now using Executors.newVirtualThreadPerTaskExecutor() to execute
tasks, so the executor is tracked by the runtime for thread dumps, etc.

Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet authored Aug 15, 2022
1 parent 32e0815 commit 6f0623f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -24,20 +25,20 @@
* and, if virtual threads are supported, to start virtual threads.</p>
*
* @see #areSupported()
* @see #startVirtualThread(Runnable)
* @see #executeOnVirtualThread(Runnable)
* @see #isVirtualThread()
*/
public class VirtualThreads
{
private static final Logger LOG = LoggerFactory.getLogger(VirtualThreads.class);
private static final Method startVirtualThread = probeStartVirtualThread();
private static final Executor executor = probeVirtualThreadExecutor();
private static final Method isVirtualThread = probeIsVirtualThread();

private static Method probeStartVirtualThread()
private static Executor probeVirtualThreadExecutor()
{
try
{
return Thread.class.getMethod("startVirtualThread", Runnable.class);
return (Executor)Executors.class.getMethod("newVirtualThreadPerTaskExecutor").invoke(null);
}
catch (Throwable x)
{
Expand Down Expand Up @@ -67,7 +68,7 @@ private static void warn()
*/
public static boolean areSupported()
{
return startVirtualThread != null;
return executor != null;
}

/**
Expand All @@ -78,13 +79,13 @@ public static boolean areSupported()
* @param task the task to execute in a virtual thread
* @see #areSupported()
*/
public static void startVirtualThread(Runnable task)
public static void executeOnVirtualThread(Runnable task)
{
try
{
if (LOG.isDebugEnabled())
LOG.debug("Starting in virtual thread: {}", task);
startVirtualThread.invoke(null, task);
executor.execute(task);
}
catch (Throwable x)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private void execute(Runnable task)
try
{
if (isUseVirtualThreads())
VirtualThreads.startVirtualThread(task);
VirtualThreads.executeOnVirtualThread(task);
else
_executor.execute(task);
}
Expand Down

0 comments on commit 6f0623f

Please sign in to comment.