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.

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-";
        public Thread newThread(Runnable r) {
            final Thread t = new Thread(r + .getAndIncrement(), 0);
            if (t.getPriority() != .) {
            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.

poolSize the number of threads to create
name the name of the pool
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));
                    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()) {
New to GrepCode? Check out our FAQ X