package io.airlift.concurrent;

import com.google.common.util.concurrent.ThreadFactoryBuilder;

import java.util.concurrent.ThreadFactory;

public final class Threads
{
    private Threads() {}

    /**
     * Creates a {@link ThreadFactory} that creates named threads
     * using the specified naming format.
     *
     * @param nameFormat a {@link String#format(String, Object...)}-compatible
     * format string, to which a unique integer will be supplied as the single
     * parameter. This integer will be unique to this instance of the
     * ThreadFactory and will be assigned sequentially.
     * @return the created ThreadFactory
     */
    public static ThreadFactory threadsNamed(String nameFormat)
    {
        return new ThreadFactoryBuilder()
                .setNameFormat(nameFormat)
                .build();
    }

    /**
     * Creates a {@link ThreadFactory} that creates named daemon threads.
     * using the specified naming format.
     *
     * @param nameFormat see {@link #threadsNamed(String)}
     * @return the created ThreadFactory
     */
    public static ThreadFactory daemonThreadsNamed(String nameFormat)
    {
        return new ThreadFactoryBuilder()
                .setNameFormat(nameFormat)
                .setDaemon(true)
                .build();
    }
}

@@ -791,32 +791,151 @@
 mat(nameFormat)%0A
+                .setThreadFactory(new ContextClassLoaderThreadFactory(Thread.currentThread().getContextClassLoader()))%0A
                 
@@ -940,24 +940,24 @@
   .build();%0A
-
     %7D%0A%0A    /
@@ -1397,29 +1397,661 @@
            .
-build();
+setThreadFactory(new ContextClassLoaderThreadFactory(Thread.currentThread().getContextClassLoader()))%0A                .build();%0A    %7D%0A%0A    private static class ContextClassLoaderThreadFactory%0A            implements ThreadFactory%0A    %7B%0A        private final ClassLoader classLoader;%0A%0A        public ContextClassLoaderThreadFactory(ClassLoader classLoader)%0A        %7B%0A            this.classLoader = classLoader;%0A        %7D%0A%0A        @Override%0A        public Thread newThread(Runnable runnable)%0A        %7B%0A            Thread thread = new Thread(runnable);%0A            thread.setContextClassLoader(classLoader);%0A            return thread;%0A        %7D
 %0A    %7D%0A%7D%0A

Set thread context class loader in thread factories