Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.yammer.metrics.core;
 
A manager class for a set of named thread pools.
 
 class ThreadPools {
    
A simple named thread factory.
    private static class NamedThreadFactory implements ThreadFactory {
        private final ThreadGroup group;
        private final AtomicInteger threadNumber = new AtomicInteger(1);
        private final String namePrefix;

        
Creates a new ThreadPools.NamedThreadFactory with the given name.

Parameters:
name the name of the threads, to be used in the pattern metrics-$NAME$-thread-$NUMBER$
        NamedThreadFactory(String name) {
            final SecurityManager s = System.getSecurityManager();
            this. = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
            this. = "metrics-" + name + "-thread-";
        }
        @Override
        public Thread newThread(Runnable r) {
            final Thread t = new Thread(r + .getAndIncrement(), 0);
            t.setDaemon(true);
            if (t.getPriority() != .) {
                t.setPriority(.);
            }
            return t;
        }
    }
            new ConcurrentHashMap<StringScheduledExecutorService>(100);

    
Creates a new scheduled thread pool of a given size with the given name, or returns an existing thread pool if one was already created with the same name.

Parameters:
poolSize the number of threads to create
name the name of the pool
Returns:
a new java.util.concurrent.ScheduledExecutorService
    ScheduledExecutorService newScheduledThreadPool(int poolSizeString name) {
        final ScheduledExecutorService existing = .get(name);
        if (isValidExecutor(existing)) {
            return existing;
        } else {
            // We lock here because executors are expensive to create. So
            // instead of just doing the usual putIfAbsent dance, we lock the
            // damn thing, check to see if anyone else put a thread pool in
            // there while we weren't watching.
            synchronized (this) {
                final ScheduledExecutorService lastChance = .get(name);
                if (isValidExecutor(lastChance)) {
                    return lastChance;
                } else {
                    final ScheduledExecutorService service =
                            Executors.newScheduledThreadPool(poolSizenew NamedThreadFactory(name));
                    .put(nameservice);
                    return service;
                }
            }
        }
    }
    private static boolean isValidExecutor(ExecutorService executor) {
        return executor != null && !executor.isShutdown() && !executor.isTerminated();
    }

    
Shuts down all thread pools created by this class in an orderly fashion.
    void shutdown() {
        synchronized (this) {
            for (ExecutorService executor : .values()) {
                executor.shutdown();
            }
            .clear();
        }
    }
New to GrepCode? Check out our FAQ X