import java.net.URL;
import java.net.URLClassLoader;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;

public class ContextClassLoaderInForkJoinPoolCommonPool {
    public static void main(String[] args) throws InterruptedException, ExecutionException {

        CountDownLatch latch = new CountDownLatch(1);

        ForkJoinPool.commonPool().execute(() -> {
            Thread thread = Thread.currentThread();

            ClassLoader originalCCL = thread.getContextClassLoader();

            // Set a custom ContextClassLoader and verify that it has been set
            ClassLoader customCCL = new URLClassLoader(new URL[0]);
            thread.setContextClassLoader(customCCL);
            if (thread.getContextClassLoader() != customCCL) {
                throw new RuntimeException("ERROR: cannot set custom ContextClassLoader");
            }

            // Restore the original ContextClassLoader and verify that it has been set
            thread.setContextClassLoader(originalCCL);
            if (thread.getContextClassLoader() != originalCCL) {
                throw new RuntimeException("ERROR: cannot reset to original ContextClassLoader");
            }

            latch.countDown();
        });

        latch.await();
    }
}