All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.shanqiang.Threads Maven / Gradle / Ivy

package io.github.shanqiang;

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

import java.util.concurrent.ThreadFactory;

import static java.util.concurrent.Executors.defaultThreadFactory;

public class Threads {
    private Threads() {}

    public static ThreadFactory threadsNamed(String nameFormat)
    {
        return new ThreadFactoryBuilder()
                .setNameFormat(nameFormat)
                .setThreadFactory(new ClassLoaderThreadFactory(Thread.currentThread().getContextClassLoader(), defaultThreadFactory()))
                .build();
    }

    private static class ClassLoaderThreadFactory
            implements ThreadFactory
    {
        private final ClassLoader classLoader;
        private final ThreadFactory threadFactory;

        public ClassLoaderThreadFactory(ClassLoader classLoader, ThreadFactory threadFactory)
        {
            this.classLoader = classLoader;
            this.threadFactory = threadFactory;
        }

        @Override
        public Thread newThread(Runnable runnable)
        {
            Thread thread = threadFactory.newThread(runnable);
            thread.setContextClassLoader(classLoader);
            return thread;
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy