001package org.anarres.parallelgzip;
002
003import java.util.concurrent.ArrayBlockingQueue;
004import java.util.concurrent.ExecutorService;
005import java.util.concurrent.Executors;
006import java.util.concurrent.ThreadFactory;
007import java.util.concurrent.ThreadPoolExecutor;
008import java.util.concurrent.TimeUnit;
009import java.util.concurrent.atomic.AtomicLong;
010import javax.annotation.Nonnegative;
011import javax.annotation.Nonnull;
012
013/**
014 *
015 * @author shevek
016 */
017public class ParallelGZIPEnvironment {
018
019    private static class ThreadFactoryHolder {
020
021        private static final ThreadFactory THREAD_FACTORY = new ThreadFactory() {
022            private final ThreadFactory defaultThreadFactory = Executors.defaultThreadFactory();
023            private final AtomicLong counter = new AtomicLong(0);
024
025            @Override
026            public Thread newThread(@Nonnull Runnable r) {
027                Thread thread = defaultThreadFactory.newThread(r);
028                thread.setName("parallelgzip-" + counter.getAndIncrement());
029                thread.setDaemon(true);
030                return thread;
031            }
032        };
033    }
034
035    @Nonnull
036    public static ThreadPoolExecutor newThreadPoolExecutor(@Nonnegative int nthreads) {
037        ThreadPoolExecutor executor = new ThreadPoolExecutor(nthreads, nthreads,
038                1L, TimeUnit.SECONDS,
039                new ArrayBlockingQueue<Runnable>(nthreads * 20),
040                ThreadFactoryHolder.THREAD_FACTORY,
041                new ThreadPoolExecutor.CallerRunsPolicy());
042        executor.allowCoreThreadTimeOut(true);
043        return executor;
044    }
045
046    private static class ThreadPoolHolder {
047
048        private static final ExecutorService EXECUTOR = newThreadPoolExecutor(Runtime.getRuntime().availableProcessors());
049    }
050
051    @Nonnull
052    public static ExecutorService getSharedThreadPool() {
053        return ThreadPoolHolder.EXECUTOR;
054    }
055}