package org.stianloader.mrjmania; import java.util.concurrent.Executor; import java.util.concurrent.ForkJoinPool; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; /** * Polyfills regarding threads, {@link Executor}s and thread pools. */ public class MRJExecutors { private MRJExecutors() { throw new UnsupportedOperationException(); } /** * Gets a {@link String} representation of the {@link #getVirtualExecutor()} * executor used. Or, it returns which pool is being used. * * @return A {@link String} representation of the {@link Executor} used. */ @NotNull @Contract(pure = true) public static String getVirtualExecutorType() { return "ForkJoinPool.commonPool"; } /** * Returns whether virtual threads are enabled/support in the given release. * * @return Returns false for Java 8 to Java 20 (as per {@link MRJMania#getMRJVersion()}), true for J21 and above. */ @Contract(pure = true) public static boolean supportsVirtualExecutors() { return false; } /** * Returns an executor that may or may not be virtual, see {@link #supportsVirtualExecutors()}. * *
Note that the returned executor will be from the {@link ForkJoinPool#commonPool()} * by default if not virtual, so it might be unwise to use it for non-short-lived tasks * else it might clog up other processes. * *
Further, if the task is indeed virtual, it will spin up a virtual thread by * task. This might not be intended/great either, depending on circumstances. * * @return The virtual executor to use. */ @NotNull @Contract(pure = true) public static Executor getVirtualExecutor() { return ForkJoinPool.commonPool()::execute; } }