Repository: micronaut-projects/micronaut-core
File: context/src/main/java/io/micronaut/scheduling/LoomSupport.java (Java)

Code before:
        MethodHandle isVirtual;
        try {
            newThreadPerTaskExecutor = MethodHandles.lookup()
                .findStatic(Executors.class, "newThreadPerTaskExecutor", MethodType.methodType(ExecutorService.class, ThreadFactory.class));
            Class<?> builderCl = Class.forName("java.lang.Thread$Builder");
            Class<?> ofVirtualCl = Class.forName("java.lang.Thread$Builder$OfVirtual");
            ofVirtual = MethodHandles.lookup()
                .findStatic(Thread.class, "ofVirtual", MethodType.methodType(ofVirtualCl));
            name = MethodHandles.lookup()
                .findVirtual(builderCl, "name", MethodType.methodType(builderCl, String.class));
            nameCount = MethodHandles.lookup()
                .findVirtual(builderCl, "name", MethodType.methodType(builderCl, String.class, long.class));
            factory = MethodHandles.lookup()
                .findVirtual(builderCl, "factory", MethodType.methodType(ThreadFactory.class));
            unstarted = MethodHandles.lookup()
                .findVirtual(builderCl, "unstarted", MethodType.methodType(Thread.class, Runnable.class));
            isVirtual = MethodHandles.lookup()
                .findVirtual(Thread.class, "isVirtual", MethodType.methodType(boolean.class));

            // This will throw if this Java doesn't support Loom, or if it does but only with
            // --enable-preview.
            unstarted1("virtual threads support probe", null, () -> {
                try {
                    // Check if the JVM supports a new enough standard library version yet doesn't
                    // really implement virtual threads internally (e.g. Espresso as of June 2025).
                    sup.set(Class.forName("java.lang.ThreadBuilders$BoundVirtualThread").isInstance(Thread.currentThread()));
                } catch (ClassNotFoundException e) {
                    // Ignore.
                }
            });

        } catch (Throwable e) {
            newThreadPerTaskExecutor = null;
            ofVirtual = null;
            name = null;
            nameCount = null;
            factory = null;
            unstarted = null;
            isVirtual = null;
            sup.set(false);
            failure = e;
        }

        SUPPORTED = sup.get();
        MH_NEW_THREAD_PER_TASK_EXECUTOR = newThreadPerTaskExecutor;
        MH_OF_VIRTUAL = ofVirtual;
        MH_NAME = name;
        MH_NAME_COUNT = nameCount;
        MH_FACTORY = factory;
        MH_UNSTARTED = unstarted;
        MH_IS_VIRTUAL = isVirtual;

Review comment (bug):
shouldn't it be the opposite? also looks like a race condition

Code after:
        try {
            newThreadPerTaskExecutor = MethodHandles.lookup()
                .findStatic(Executors.class, "newThreadPerTaskExecutor", MethodType.methodType(ExecutorService.class, ThreadFactory.class));
            Class<?> builderCl = Class.forName("java.lang.Thread$Builder");
            Class<?> ofVirtualCl = Class.forName("java.lang.Thread$Builder$OfVirtual");
            ofVirtual = MethodHandles.lookup()
                .findStatic(Thread.class, "ofVirtual", MethodType.methodType(ofVirtualCl));
            name = MethodHandles.lookup()
                .findVirtual(builderCl, "name", MethodType.methodType(builderCl, String.class));
            nameCount = MethodHandles.lookup()
                .findVirtual(builderCl, "name", MethodType.methodType(builderCl, String.class, long.class));
            factory = MethodHandles.lookup()
                .findVirtual(builderCl, "factory", MethodType.methodType(ThreadFactory.class));
            unstarted = MethodHandles.lookup()
                .findVirtual(builderCl, "unstarted", MethodType.methodType(Thread.class, Runnable.class));
            isVirtual = MethodHandles.lookup()
                .findVirtual(Thread.class, "isVirtual", MethodType.methodType(boolean.class));

            // This will throw if this Java doesn't support Loom, or if it does but only with
            // --enable-preview.
            Thread probe = (Thread) unstarted.invoke(ofVirtual.invoke(), (Runnable) () -> { });

            // This checks if the JVM actually creates real virtual threads, or if it uses
            // 'bound threads' which are just platform threads. As of June 2025 the Espresso JVM
            // falls into this category. Checking here voids casting issues later in code that
            // makes assumptions about the internals.
            sup = Class.forName("java.lang.VirtualThread").isInstance(probe);
            if (!sup) {
                failure = new Exception("This JVM doesn't fully implement virtual threads and produces regular platform threads instead.");
            }
        } catch (Throwable e) {
            newThreadPerTaskExecutor = null;
            ofVirtual = null;
            name = null;
            nameCount = null;
            factory = null;
            unstarted = null;
            isVirtual = null;
            sup = false;
            failure = e;
        }

        SUPPORTED = sup;
        MH_NEW_THREAD_PER_TASK_EXECUTOR = newThreadPerTaskExecutor;
        MH_OF_VIRTUAL = ofVirtual;
        MH_NAME = name;
        MH_NAME_COUNT = nameCount;
        MH_FACTORY = factory;
        MH_UNSTARTED = unstarted;
        MH_IS_VIRTUAL = isVirtual;
    }